Ruby on rails - Pass text_field's value in form to a parameter for an action in a different controller
I have a form which has this text field
<%= f.text_field :content %>
I have a link_to tag to post the value of the text field to an action present in another controller. I need to be able to get the value of the text field and say
<%= link_to 'post', :controller => "a_different_controller", :action => "update", :message => "text field's value" %>
Can you please hel开发者_Go百科p me out here?
I tried various options posted on stack overflow. None seem to work.
If you want to POST a value without a html form, you must do this via javascript. You can create a onClick listener for that link, and in the listener grab the value of that text_field and submit the form via javascript.
You can pass the remote
and method
options on your link_to
helper:
<%= link_to 'Post', yourmodel_path, :remote => true, :method => :put %>
That will submit it via ajax. If you don't want that and you just want to use the PUT HTTP verb, you can drop the :remote => true
portion.
Note that I'm using the shorthand way of referring to the action you are targeting. If you're not familiar with that, I suggest you run through the Rails Guides…it's pretty core to understanding example code.
精彩评论