0

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

1
  • 1
    Why do you want it as part of the submit button and not as, say, a hidden form field? Commented Dec 10, 2014 at 22:56

1 Answer 1

5

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].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.