【发布时间】: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