I am getting the ActiveModel::ForbiddenAttributesError with friendly_id and for the life of me cannot work out why. I believe I have configured my strong params as per normal and even tried modifying them multiple times to see what would happen.. I still get the error.
Please find the code below:
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
Does anyone know what's going on? I have looked over documentation but can't see anything. I have the field :slug added into my schema:
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
As per comment request here is my form:
<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>
Here is the error message:
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'
And this is the params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JhF5bj4QU9Km0KfywdL0iCAZxczHd2MlF0FXSPtD47o=", "post"=>{"title"=>"asdfasdf", "body"=>"asdfasdf"}, "commit"=>"Create Post"}