Rails radio_button_tag how to send multiple parame

2019-09-09 21:38发布

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

2条回答
做个烂人
2楼-- · 2019-09-09 22:09

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
查看更多
Bombasti
3楼-- · 2019-09-09 22:15

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]
查看更多
登录 后发表回答