Miguel Morujão
Miguel Morujão

Reputation: 1161

Custom parameters from view to controller

i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object @event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.

 <%= form_tag logic_giveRandomGifts_path :method => 'post' %>
          <div class="form-group">
            <%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
            Quantidate:
            <%= text_field_tag :quantity, params[:quantity], :size => 2 %>
            <%= submit_tag "GO!",params[:event_id] => @event.id,:class => 'btn btn-default'  %>
          </div>

Upvotes: 0

Views: 89

Answers (3)

Rokibul Hasan
Rokibul Hasan

Reputation: 4156

Simply pass an hidden field in form as followings

<%= hidden_field_tag :event_id, value: @event.id%>

It will available in controller as

 params[:event_id]

Upvotes: 1

Gaurav Gupta
Gaurav Gupta

Reputation: 1191

Add hidden filed in your form and set its value equal to @event.id

 <%= form_tag logic_giveRandomGifts_path :method => 'post' %>
  <div class="form-group">
    <%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
    Quantidate:
    <%= text_field_tag :quantity, params[:quantity], :size => 2 %>
    <%= hidden_field_tag :event_id, value: @event.id%> #add this
    <%= submit_tag "GO!",params[:event_id] => @event.id,:class => 'btn btn-default'  %>
  </div>
<% end %>

You can use now event id in your controller as params[:event_id]

Upvotes: 3

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

<%= hidden_field_tag :event_id, @event.id %>

So this will be available in params[:event_id].

Upvotes: 2

Related Questions