问题
I'm trying to figure out, if it possible to send multiple parameters with radio button.
First parameter will be time
and second provider_id
. I'm using each method for providers, and trying to have option to select only one time and only from one provider.
My view form_tag
:
<%= form_tag(provider_order_create_path) %>
<div class="btn-group" data-toggle="buttons">
<% @group.provider.each do |provider| %>
<label class="btn btn-default">
<%= radio_button_tag :order, time_for_order_00_00.to_datetime, 'false',
{:provider_id => provider.id} %><%= time_tag(time_00_00, :format => '%I:%M %p') %>
</label>
<% end %>
</div>
<%= submit_tag "Submit", type: 'submit', :name => nil %>
At this time i'm seeing only :order params in the logs :provider_id
"order"=>"2016-03-09T00:00:00+00:00"
I tried to use hidden_field
but it not working as expected it is always sending id for last provider in the list
Thank you, any help appreciated
回答1:
Send an array from the rado button tag, and fetch using the index value in the controller.
<%= radio_button_tag :order, [time_for_order_00_00.to_datetime,provider.id], 'false', {:provider_id => provider.id} %><%= time_tag(time_00_00, :format => '%I:%M %p') %> , controller_action => params[:order[0]] ,params[:order[1]]
In the controller,
def you_method
@time = params[:order].split[0]
@provider = params[:order].split[1]
end
回答2:
I was able to figure it out with help of @Sravan.
View will looks like:
<%= radio_button_tag :order, [time_for_order_00_00.to_datetime,provider.id],
'false', {:provider_id => provider.id} %><%= time_tag(time_00_00, :format => '%I:%M %p') %>
and
Controller will be:
@time_from = params[:order].split[0]
@provide_id = params[:order].split[1]
来源:https://stackoverflow.com/questions/35861158/rails-radio-button-tag-how-to-send-multiple-parameters