【问题标题】:How to pass an argument into a controller method from a form_tag如何将参数从 form_tag 传递到控制器方法
【发布时间】:2015-04-21 22:09:50
【问题描述】:

在我的用户显示视图中:

<%= form_tag assign_as_student_path do %>
    <%= hidden_field_tag :user_id, @user.id %>
    <%= submit_tag "Assign as a Student", data: { confirm: "Are you sure?" } %>
<% end %>

TeachersController(继承自 UsersController):

def assign_as_student
    @user = User.find(params[:user_id])
    @user.update_attributes(type: "Student")
    @user.save
    redirect_to admin_path, notice: "They are now a Student" 
end

routes.rb:

post 'assign_as_student' => 'teachers#assign_as_student'

好的,这可行。但是……

这是我不得不接受的,因为我不知道如何将参数传递给控制器​​方法。

我想像上面那样在 UsersController 中编写一个方法,但使用这样的参数(我认为这可行,但无法对其进行测试):

def assign_as_type(type)
    @user = User.find(params[:user_id])
    @user.update_attributes(type: type)
    @user.save
    redirect_to admin_path, notice: "They are now a Student" 
end

这样,我就可以只使用这种方法,而不是使用“assign_as_student”方法和“assign_as_teacher”方法。干爽总是好的。

所以...我认为上述方法可行,但我将如何从表单中传递参数?类似于下面的表格,但添加了一些可以传递字符串“Student”或“Teacher”的内容:

<%= form_tag assign_as_type_path do %>
  <%= hidden_field_tag :user_id, @user.id %>
  <%= submit_tag "Assign as a (Student/Teacher), data: { confirm: "Are you sure?" } %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails forms ruby-on-rails-4


    【解决方案1】:

    (在这个答案中,我假设您使用的是 Rails 4+,否则我的一些建议可能不适用,最好在问题中提供这个。)

    要将另一个参数传递给控制器​​,您可以这样做:

    <%= form_tag assign_as_type_path do %>
      <%= hidden_field_tag :user_id, @user.id %>
      <%= select_tag :type, options_for_select([ "Student", "Teacher" ]) %>
      <%= submit_tag "Assign", data: { confirm: "Are you sure?" } %>
    <% end %>
    

    然后应该可以通过params[:type] 访问该参数。

    由于这是对 @user-object 的操作,我认为您应该这样做:

    <%= form_for @user, url: assign_as_type_path do |f[ %>
      <%= f.select :type, options_for_select([ "Student", "Teacher" ],@user.type) %>
      <%= f.submit "Assign", data: { confirm: "Are you sure?" } %>
    <% end %>
    

    在你的控制器中,我认为你应该这样做:

    (这里假设你改成form_for

    def assign_as_type(type)
      @user = User.find(params[:id])
      if @user.update(type: params[:user][:type]) # This sets attribute + saves
        redirect_to admin_path, notice: "They are now a " + @user.type
      else
        redirect_to admin_path, error: "User could not be updated."
      end    
    end
    

    这样,您可以使用模型验证,并且如果您像在正常更新操作中一样执行重定向,您还可以在表单中显示错误。

    希望对你有帮助,如果有任何问题,尽管问?

    params更新答案

    我理解 params 的方式(并了解是否有问题)是通过查看服务器日志中的最新请求(如果您在本地运行它,它就是您开始的地方 rails s)。

    在这里你会看到:

    params: { user_id: 1, type: "Student" , commit: "Assign"}
    

    还有一些额外的参数。

    这是一个普通的 Ruby 哈希,称为 params。要进入 Ruby 语言,您可以执行以下操作:http://tryruby.org/ 教程?

    如果您将其更改为 form_for,您的参数哈希将如下所示:

    params: { user: { id: 1, type: "Student" }, commit: "Assign" }
    

    参考: http://ruby-doc.org/core-2.2.0/Hash.html

    【讨论】:

    • 尚未尝试,但它看起来很棒。我想在实现它之前完全了解发生了什么,为此我需要阅读参数。我理解那里发生的一切。这是我不明白的方法中的第三行。我仍然不太了解参数。有什么阅读建议吗?似乎很难找到对中间体参数的一个很好的解释。
    猜你喜欢
    • 2014-12-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多