无法识别Task_params,我不确定为什么。我已经看了很多遍了,但仍然无法解决。尽管我在create中使用task_params,但引发错误的代码部分恰好位于底部。我对此并不陌生,所以希望我不会错过任何显而易见的事情!我正在使用Ruby on Rails。谢谢。意识到我犯了一个非常愚蠢的错误。我正在关注使用模型任务的教程。因此,我实际上不应该使用任务参数,而应该使用模型Im!
class ModuleListsController < ApplicationController
before_action :set_module_list, only: [:show, :edit, :update, :destroy]
before_action :set_student, only: [:new, :create]
# GET /module_lists
# GET /module_lists.json
def index
@module_lists = ModuleList.all
end
# GET /module_lists/1
# GET /module_lists/1.json
def show
end
# GET /module_lists/new
def new
@module_list = @student.module_lists.new
end
# GET /module_lists/1/edit
def edit
end
# POST /module_lists
# POST /module_lists.json
def create
@module_list = @student.module_lists.new(task_params)
respond_to do |format|
if @module_list.save
format.html { redirect_to @module_list, notice: 'Module successfully created.' }
format.json { render :show, status: :created, location: @module_list }
else
format.html { render :new }
format.json { render json: @module_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /module_lists/1
# PATCH/PUT /module_lists/1.json
def update
respond_to do |format|
if @module_list.update(module_list_params)
format.html { redirect_to @module_list, notice: 'Module list was successfully updated.' }
format.json { render :show, status: :ok, location: @module_list }
else
format.html { render :edit }
format.json { render json: @module_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /module_lists/1
# DELETE /module_lists/1.json
def destroy
@module_list.destroy
respond_to do |format|
format.html { redirect_to module_lists_url, notice: 'Module list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_module_list
@module_list = ModuleList.find(params[:id])
end
def module_list_params
params.require(:module_list).permit(:student_id, :title, :description, :credit_value)
end
def set_student
@student = Student.find_by(id: params[:student_id]) ||
Student.find(task_params[:student_id])
end
end
那是因为您没有task_params
首先定义。添加以下内容后,它应该可以工作:
def task_params
params.require(:module_list).permit(:content, :user_id)
end
这必须转到private
文件末尾的您的部分。放在圆括号之间的内容permit
取决于您要求用户通过表单提交的元素。每个元素都必须被允许,否则它不会被存储到数据库中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句