我正在使用Rails 4和Ruby 2,我正在尝试修复一个在创建新记录时给我一个Forbidden Attributes错误的错误。
这是我的控制器中的创建代码:
def create
@instructor = Instructor.new(instructor_params)
respond_to do |format|
if @instructor.save
format.html { redirect_to @instructor, notice: 'Instructor was successfully created.' }
format.json { render json: @instructor, status: :created, location: @instructor }
else
format.html { render action: "new" }
format.json { render json: @instructor.errors, status: :unprocessable_entity }
end
end
end
我强大的参数代码:
def instructor_params
params.require(:instructor).permit(:bio, :hometown, :name, :school, :sort_order, :started_sailing, :started_teaching, :photo)
end
这是我的instructor.rb文件:
class Instructor < ActiveRecord::Base
validates_presence_of :bio, :name
mount_uploader :photo, PhotoUploader
def experience
distance_of_time_in_words_to_now (self.started_sailing)
end
end
我得到的错误是:
ActiveModel::ForbiddenAttributesError in InstructorsController#create
ActiveModel::ForbiddenAttributesError
Rails.root: /Users/scottsipiora/Sites/clycss
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"l2159VRjW9dUluWPJBxsubEt7t37CztXXVX/kjEpGfs=",
"instructor"=>{"name"=>"Libby",
"bio"=>"Bio content",
"sort_order"=>"",
"photo"=>#<ActionDispatch::Http::UploadedFile:0x007fd6aad96ec0 @tempfile=#<Tempfile:/var/folders/3k/fnl40lzs2v55jvj8yj6y07gh0000gn/T/RackMultipart20140706-8616-1mzzdxw>,
@original_filename="Libby2.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"instructor[photo]\"; filename=\"Libby2.jpg\"\r\nContent-Type: image/jpeg\r\n">},
"commit"=>"Create Instructor"}
我正在使用cancan进行授权,并找到了此页面: https://github.com/ryanb/cancan/issues/835
我尝试了下面显示的AntonTrapp发布的解决方法,但这也没有用。
before_filter do
resource = controller_name.singularize.to_sym
method = "#{resource}_params"
params[resource] &&= send(method) if respond_to?(method, true)
end
知道我做错了什么吗?我很难过。
提前致谢。