Newb RoR developer here.
I have a model called size.rb
that basically has a variety of size elements.
Now, in my subsequent view, I have multiple sets of radio_button_tags
. Since each set is independent, I renamed the each ( shirtsize_ids
, waistsize_ids
, etc)
Now, the issue I'm having is how to pass all of these different size_id
parameters to the controller. At the end of the day, they are all recorded as a size_id
attribute in a joint-model called usersize, but since I had to differentiate them in the view (for radio button purposes), I'm stuck!
Thanks for the help
view
<% Size.select { |size| size.category == 'waist' }.sort.each do |size| %>
<%= radio_button_tag 'waistsize_ids[]', size.id, false, :id => "waistsizing-#{size.id}" %>
<% Size.select { |size| size.category == 'waist' }.sort.each do |size| %>
<%= radio_button_tag 'inseamsize_ids[]', size.id, false, :id => "inseamsizing-#{size.id}" %>
controller
def create
@user_size = UserSize.new(params[:user_size])
params = {"waistsize" => {'id' => 'size.id'}}
params.each do |size|
UserSize.create(:size_id => size.id, :user_id => current_user.id)
end
end
radio_button
versus radio_button_tag
might be a better choice:
<%= radio_button :waistsize, :id, size.id, :checked => false, :id => "waistsizing-#{size.id}" %>
<%= radio_button :inseamsize, :id, size.id, :checked => false, :id => "inseamsizing-#{size.id}" %>
# => params = { ..., "waistsize"=>{"id"=>"xyz"}, "inseamsize"=>{"id"=>"xyz"} }
Then, in the controller:
def create
@user_size = UserSize.new(params[:user_size])
UserSize.create(:size_id => params[:waistsize][:id], :user_id => current_user.id)
UserSize.create(:size_id => params[:inseamsize][:id], :user_id => current_user.id)
end
UserSize模型是否类似于'size_type',或者为什么你有'waistsize'和'inseamsize'等等。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.