Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate hidden form field data with rails button_to helper?

Currently, i use the following code for passing params[:id] to line_items create

<%= button_to 'Add to Cart', { controller: 'line_items', action: 'create', id: brick },  class: 'green radius nice button', method: :post, remote: true %>

However, it will append the id param on url query which i didn't suppose button_to to do. What i want is passing the id param via hidden the form field. Can i do that in button_to or any workaround for this issue?

like image 495
Tim Avatar asked Feb 08 '12 04:02

Tim


1 Answers

Starting with Rails 4.1.0, button_to has now the additional options :params which can receive a hash that will be transformed into hidden fields. For example:

button_to 'Send Invitation', invitation_path(user), params: {'user[email]' => user.email}

Will give :

<form action="/users/invitation" class="button_to" method="post">
   <div>
       <input type="submit" value="Resend">
       <input name="user[email]" type="hidden" value="[email protected]">
   </div>
</form>
like image 55
Whyves Avatar answered Oct 08 '22 07:10

Whyves