开发者

Ruby on rails - Pass text_field's value in form to a parameter for an action in a different controller

开发者 https://www.devze.com 2023-02-16 05:58 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消