Reputation: 49
I am new to Ruby on Rails and I was trying to create a simple app when I ended up having a ActiveModel::ForbiddenAttributesError
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post =Post.new
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to post_path,:notice=>"success"
else
render "new"
end
end
def edit
end
def update
end
def destroy
end
private
def post_params
params.require(:post).permit(:Title, :content)
end
end
I have seen a similar error here but the solution for that did not fix my issue.
My version of rails is 4.2.0.
The error displayed is
Upvotes: 0
Views: 555
Reputation: 1
def create
@post = Post.new(posts_params)
if @post.save
redirect_to post_path,:notice=>"success"
else
render "new"
end
end
private
def posts_params
params.require(:post).permit(:Title, :content)
end
Upvotes: 0
Reputation: 21
I think that
def create
@post = Post.new post_params
if @post.save
flash[:success] = "Created new post"
redirect_to @post
else
render 'new'
end
end
Upvotes: 0
Reputation: 27961
You can't use that params[:post]
hash (or any params[*]
hash) directly in any mass-assignment method, you need to use a permit
call so Rails knows you've checked it and to allow it.
So, change your Post.new
to @post = Post.new(post_params)
Upvotes: 1
Reputation: 13067
Change @post = Post.new(params[:post])
to @post = Post.new(post_params)
.
Upvotes: 1