我试图创建一个选择,但是我收到了这个错误:
ActionController :: ClassesController中的ParameterMissing #create
param丢失或值为空:class#Never never trust parameters 从恐怖的互联网上,只允许通过白名单。
def class_params
params.require(:class).permit(:classe,:segmento_id)
端
端
我试图创建一个选择,但同样的是返回此错误...如果它是一个简单的选择。我使用的是simple_form。
有人帮我吗?
我的表格:
<%= simple_form_for(@class) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :classe %>
<%= f.collection_select :segmento_id, Segmento.all, :id, :categoria, prompt: 'Selecione uma Categoria', input_html: { class:'form-control' } %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
我的控制器:
class ClassesController < ApplicationController
before_action :set_class, only: [:show, :edit, :update, :destroy]
# GET /classes
# GET /classes.json
def index
@classes = Classe.all
end
# GET /classes/1
# GET /classes/1.json
def show
end
# GET /classes/new
def new
@class = Classe.new
end
# GET /classes/1/edit
def edit
end
# POST /classes
# POST /classes.json
def create
@class = Classe.new(class_params)
respond_to do |format|
if @class.save
format.html { redirect_to @class, notice: 'Classe was successfully created.' }
format.json { render :show, status: :created, location: @class }
else
format.html { render :new }
format.json { render json: @class.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /classes/1
# PATCH/PUT /classes/1.json
def update
respond_to do |format|
if @class.update(class_params)
format.html { redirect_to @class, notice: 'Classe was successfully updated.' }
format.json { render :show, status: :ok, location: @class }
else
format.html { render :edit }
format.json { render json: @class.errors, status: :unprocessable_entity }
end
end
end
# DELETE /classes/1
# DELETE /classes/1.json
def destroy
@class.destroy
respond_to do |format|
format.html { redirect_to classes_url, notice: 'Classe was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_class
@class = Classe.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def class_params
params.require(:class).permit(:classe, :segmento_id)
end
end
答案 0 :(得分:1)
尝试将class
重命名为class
以外的其他内容。
事实上,尽量避免使用关键字作为字段,属性,方法,类名等。它会混淆语言运行时,框架和其他开发人员。
答案 1 :(得分:1)
params.require(:classe)中有拼写错误
def class_params
params.require(:classe).permit(:classe, :segmento_id)
end