我正在使用friendly_id获取ActiveModel :: ForbiddenAttributesError,并且在我的生活中无法解决原因。我相信我按照正常情况配置了我的强力参数,甚至尝试多次修改它们以查看会发生什么......我仍然得到错误。
请找到以下代码:
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
validates_presence_of :body, :title, :slug
has_many :comments
end
class PostsController < InheritedResources::Base
load_and_authorize_resource
def new
@post = Post.new
end
def create
@post = Post.create(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @posts }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def show
@post = Post.friendly.find(params[:id])
end
def index
@posts = Post.all
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, :slug, :body)
end
end
有谁知道发生了什么?我查看了文档,但看不到任何内容。我有字段:slug添加到我的架构中:
create_table "posts", force: true do |t|
t.string "title"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
end
add_index "posts", ["slug"], name: "index_posts_on_slug", unique: true, using: :btree
根据评论请求,这是我的表格:
<div class='panel panel-default'>
<div class='panel-heading'>
<h2>New post</h2>
</div>
<div class='panel-body'>
<div class="container">
<%= semantic_form_for @post, :html => {:class => 'main-form'} do |f| %>
<div class='row'>
<div class="col-xs-12">
<%= f.input :title, :input_html => { :class => 'form-control input-box', :placeholder => 'Title', :autofocus => true } %>
</div>
</div>
<div class='row'>
<div class="col-xs-12">
<%= f.input :body, :input_html => { :class => 'form-control input-box', :placeholder => 'Body' } %>
</div>
</div>
<div>
<%= f.submit "Create Post", class: 'btn standard-button' %>
</div>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
</div>
以下是错误消息:
ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError:
activemodel (4.1.0) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
activerecord (4.1.0) lib/active_record/attribute_assignment.rb:24:in `assign_attributes'
activerecord (4.1.0) lib/active_record/core.rb:452:in `init_attributes'
activerecord (4.1.0) lib/active_record/core.rb:198:in `initialize'
activerecord (4.1.0) lib/active_record/inheritance.rb:30:in `new'
activerecord (4.1.0) lib/active_record/inheritance.rb:30:in `new'
inherited_resources (1.4.1) lib/inherited_resources/base_helpers.rb:59:in `build_resource'
cancancan (1.9.2) lib/cancan/inherited_resource.rb:9:in `load_resource_instance'
cancancan (1.9.2) lib/cancan/controller_resource.rb:32:in `load_resource'
这就是参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JhF5bj4QU9Km0KfywdL0iCAZxczHd2MlF0FXSPtD47o=", "post"=>{"title"=>"asdfasdf", "body"=>"asdfasdf"}, "commit"=>"Create Post"}