Reddirt
Reddirt

Reputation: 5953

Rails pass parameter with submit

I have a Rails 3.2.12 app where I would like to pass a parameter via a form submit button.

The param is :invoice_id. In the form the value is in @invoice.

I tried this:

<%= f.submit "Submit", :class => "btn btn-success", params: {:invoice_id => @invoice} %>

In the controller, I would access it like this:

def addinvtimes
  @invoice = params[:invoice_id]

But, it ends up being nil.

Thanks for the help!

UPDATE1

Upvotes: 0

Views: 6578

Answers (1)

Todd Agulnick
Todd Agulnick

Reputation: 1995

That's not how HTML forms work. If there's data that you want to get submitted along with the rest of your form's data but not be viewable or editable by the user, stuff it into a hidden field, like so:

<%= form_for @order do |f| %>
  <%= f.text_field :customer_name %>
  <%= f.hidden_field :invoice_id, value: @invoice.id %>
<% end %>

When you do this, the invoice_id will be submitted alongside the rest of the form's data, so in this case you would access it as params[:order][:invoice_id].

Upvotes: 5

Related Questions