我克隆了一些源代码来研究这个railscast(https://github.com/railscasts/253-carrierwave-file-uploads)。当时可能使用不推荐的attr_accessible方法完成了Rails 3。
我正在使用Rails 4,所以我在模型中注释了attr_accessible方法
class Gallery < ActiveRecord::Base
#attr_accessible :name
has_many :paintings
end
我在控制器中添加了一个私有部分,并使用了适当的方法
private
def gallery_params
params.require(:gallery).permit(:name)
end
它应该是straithgforward但我仍然得到这个错误。
ActiveModel::ForbiddenAttributesError
任何新鲜的眼睛能帮助我吗?感谢。
EDIT 这是控制器的完整源代码
class GalleriesController < ApplicationController
def index
@galleries = Gallery.all
end
def show
@gallery = Gallery.find(params[:id])
end
def new
@gallery = Gallery.new
end
def create
@gallery = Gallery.new(params[:gallery])
if @gallery.save
flash[:notice] = "Successfully created gallery."
redirect_to @gallery
else
render :action => 'new'
end
end
def edit
@gallery = Gallery.find(params[:id])
end
def update
@gallery = Gallery.find(params[:id])
if @gallery.update_attributes(params[:gallery])
flash[:notice] = "Successfully updated gallery."
redirect_to gallery_url
else
render :action => 'edit'
end
end
def destroy
@gallery = Gallery.find(params[:id])
@gallery.destroy
flash[:notice] = "Successfully destroyed gallery."
redirect_to galleries_url
end
private
def gallery_params
params.require(:gallery).permit(:name)
end
end
和Gemfile的源代码
source 'https://rubygems.org'
gem 'rails', '4.2.5'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'protected_attributes'
gem 'bcrypt', '~> 3.1.7'
group :development, :test do
console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
gem 'spring'
end