我正在使用设计和导轨4.我有以下型号:
class Video
include Mongoid::Document
include Mongoid::Timestamps
field :url, type: String
field :video_id, type: String
field :title, type: String
field :description, type: String
field :suggested_by_name, type: String
field :suggested_by_email, type: String
field :active, type: Boolean, default: false
end
使用以下控制器:
class Frontend::VideosController < ApplicationController
before_action :authenticate_user!, only: [:index, :edit, :update, :destroy]
def index
end
def new
@videos = Video.new
end
def create
if @person = Video.create(params[:video])
flash.now[:success] = "Thanks for suggest this video"
redirect_to root_path
else
flash.now[:error] = "Impossible to add this suggestion"
redirect_to new_frontend_video_path
end
end
def edit
end
def update
end
def destroy
end
private
def video_params
params.require(:video).permit(:url, :title, :description, :suggested_by_name, :suggested_by_email)
end
end
我正试图在我的mongodb上保存一条记录,但我得到了:
::加载ActiveModel ForbiddenAttributesError
所以我认为我在强参数中犯了一个错误,请帮忙。
答案 0 :(得分:0)
在create
操作更改params[:video]
至video_params
:
def create
if @person = Video.create(video_params)
flash.now[:success] = "Thanks for suggest this video"
redirect_to root_path
else
flash.now[:error] = "Impossible to add this suggestion"
redirect_to new_frontend_video_path
end
end
您应该permit
参数,而不是Rails
加注ActiveModel::ForbiddenAttributesError
。