Reputation: 33
how to insert parameter value manually from controller, this my controller
def new
@new_post = Post.new(params[:title])
end
def create
@new_post = Post.new(new_post_params)
if @new_post.save
flash[:success] = 'Post created!'
redirect_to home_url
else
flash[:danger] = @new_post.errors.full_messages.to_sentence
redirect_to home_url
end
end
private
def new_post_params
params.require(:post).permit(
:title,
poster_id: current_user.id,
poster_name: current_user.name
)
end
my form view like this
form_for @new_post do |f|
f.text_area :title
end
tired using this method, poster_id and poster_name still blank
def new_post_params
params.require(:post).permit(
:title,
poster_id: current_user.id,
poster_name: current_user.name
)
end
Upvotes: 1
Views: 423
Reputation: 681
Try this
params.require(:post).permit(:title).merge(
{
poster_id: current_user.id,
poster_name: current_user.name
}
)
Upvotes: 2
Reputation: 6100
def new_post_params
params[:post][:poster_id] = current_user.id
params[:post][:poster_name] = current_user.name
params.require(:post).permit(
:title,
:poster_id,
:poster_name
)
end
Upvotes: 1