简体   繁体   English

将文本字段值传递给Rails Controller

[英]Passing text field value to Rails Controller

I'm fairly new to Rails and I'm stuck with passing a text field value to Rails Controller method. 我对Rails还是很陌生,我一直坚持将文本字段值传递给Rails Controller方法。 Any help is appreciated, below is my code.. 感谢您的帮助,以下是我的代码。

View code: 查看代码:

Upon click of the below button I want to be able to send the username to the Controller method.. 单击下面的按钮后,我希望能够将用户名发送到Controller方法。

<%= link_to "My button", admin_user_creds_path(user_config),
        method: :test_user, role: "button" %>

The username is entered in a text field: 用户名在文本字段中输入:

<%= form_for [:admin, user_config] do |f| %>
  <td class="js-editable-text rf-table__td rf-table__col--username">
  <div class="preview js-preview show"><%= user_config.username %></div>
  <div class="js-input hide"><%= f.text_field :username, class: "form-control" %></div>
</td>

My routes file: 我的路线文件:

post "/user_configurations/:id", to: "user_configurations#test_user", as: "user_creds"

My controller: 我的控制器:

def test_user
  username = user_params[:username]
end
def user_params
  params.require(:user_configurations).permit(:username)
end

Instead of using link_to helper you need add a submit button to your form. 无需使用link_to助手,您需要在表单中添加一个提交按钮。

<%= form_for [:admin, user_config] do |f| %>
  <td class="js-editable-text rf-table__td rf-table__col--username">
  <div class="preview js-preview show"><%= user_config.username %></div>
  <div class="js-input hide"><%= f.text_field :username, class: "form-control" %></div>
  <%= f.submit "My button" %>
</td>

Also please note that method option in link_to helper is either get or post HTTP methods but not the controller action 还请注意, link_to帮助器中的method选项是getpost HTTP方法,而不是控制器操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM