【问题标题】:param is missing or the value is empty error参数丢失或值为空错误
【发布时间】:2014-10-14 01:43:13
【问题描述】:

我在使用 params.require().permit() 时遇到问题。我有一个来自设计的用户模型和一个代表一个类的组模型(如在学术类中)。当用户在组索引视图上时,我希望他们能够单击加入并成为该组的一部分。

<%= form_for @student, :url => students_path(@student), method: :post do %>
    <%= hidden_field :student_id, :value => current_user.id %>
    <%= hidden_field :course_id, :value => group.id %>
    <%= submit_tag "+ Join", :class => "btn btn-primary pull-right join-button" %>
<% end %>

我的想法是我会将当前用户 ID 以及他们单击加入的组 ID 作为隐藏值传递,这样就只有一个加入按钮。

我的组控制器索引方法是这样的

def index
    @groups = Group.all
    @student = Student.new
end

我的学生控制器如下所示:

class StudentsController < ApplicationController
  before_action :set_student, only: [:show, :edit, :update, :destroy]

  def index
    #@students = Student.all
    @students = Student.where(:student_id => current_user.id)
    #respond_with(@students)
  end

  def show
    #respond_with(@student)
    @students = Student.find(params[:id])
  end

  def new
    @student = Student.new
    #respond_with(@student)
  end

  def edit
  end

  def create
    @student = Student.new(student_params)
    @student.save
    #respond_with(@student)
  end

  def update
    @student.update(student_params)
    #respond_with(@student)
  end

  def destroy
    @student.destroy
    #respond_with(@student)
  end

  private
    def set_student
      @student = Student.find(params[:id])
    end

    def student_params
      params.require(:student).permit(:course_id, :student_id)
    end
end

每当我尝试提交表单(也就是单击加入按钮)时,我都会收到一条错误消息:

param is missing or the value is empty: student

我也得到了这个信息,就学生和团体 ID 而言是正确的

Request

Parameters:

{"authenticity_token"=>"/bJYZBGr6lfAzb9mYnvRfMZII+QS8iskd0MRuHh+RnE=",
 "course_id"=>"10",
 "student_id"=>"4"}

这也确实插入到数据库中,但 student_id 和 course_id 为 nil。我猜这与强大的参数有关,但我不确定我做错了什么。

【问题讨论】:

  • 当你点击join按钮时它正在重定向哪个动作

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


【解决方案1】:

看起来 form_for 块中缺少您的 form_builder 对象。试试这个:

<%= form_for @student, :url => students_path(@student), method: :post do |f| %>
<%= f.hidden_field :student_id, :value => current_user.id %>
<%= f.hidden_field :course_id, :value => group.id %>
<%= submit_tag "+ Join", :class => "btn btn-primary pull-right join-button" %>
<% end %>

如果没有构建器,我认为 rails 不会在提交的参数中提供学生哈希

【讨论】:

  • 哇!我很惊讶这是一个如此简单的答案。非常感谢你的帮助!现在效果很好!
  • 你的表单和ajs4550的表单一样。区别在哪里?
  • @parzival 注意到 op 的形式确实传递了块变量 |f|并且声明表单字段的行不以 f.[field_name] 为前缀
【解决方案2】:

在之前的_action set_student
你正在使用 param[:id] ,而你的 params 没有它.. 我认为你应该使用 param[:student_id]

【讨论】:

  • 对不起,你能再澄清一下吗?我没有看到我在 before_action set_student 的哪个位置使用了 param[:id]。
  • 好的,我现在真的明白你在说什么了。我认为这是正确的,因为学生将拥有参数 ID,因为它更像是一个关系数据库,将用户与课程联系起来
  • 数据库中的学生将具有 id .. 但您在请求中以名称“student_id”传递它,所以它应该是 ... def set_student @student = Student.find(params[ :student_id]) end ... 只找到不带 fint_by_... 将通过 id 找到
  • 当您访问小组索引页面时,还没有学生。我做 Student.new ,我认为它会创建一个新学生并为其设置一个 ID,但由于我没有点击加入,它还不知道 student_id 吗?
  • you make before action 仅适用于 [:show, :edit, :update, :destroy] 它不适用于索引操作 .. 正如您提到的那样.. 甚至在索引操作中.. 你是否在 get 请求中传递了 param[:id] ?
【解决方案3】:

由于您要传递@student 路径不只是student_path(@student) 而不是您拥有的路径,即students_path(@student)?

【讨论】:

  • 当我这样做并转到 localhost:3000/groups 时,我收到此错误。 "没有路线匹配 {:action=>"show", :controller=>"students", :id=>nil} 缺少必需的键:[:id]"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-07
相关资源
最近更新 更多