Search code examples
ruby-on-rails-4strong-parameters

Rails 4 unpermitted params that should be permitted


I'm a bit stuck with this one. I'm sure it's simple, but I still can't figure it out.

Here's my permit method:

def post_params
  params.require(:post).permit(:char_id, :text).merge!(topic_id:params[:topic_id], user_id: current_user.id, ip: request.remote_ip)
end

Here's the controller update method the request is routed to:

def update
  @post.update(post_params)
  respond_to do |format|
    format.html { redirect to  }
    format.json { render partial: "post", locals: {post:@post} }
  end
end

Here's the params hash that comes with the request.
I need to get something like this.

And still when I look in the console I see this:

Unpermitted parameters: id, text, char_id, ip, show_text, posted_at, char, editable, deletable, commentable, user

Is it something working wrong or something I missed?


Solution

  • I believe you're close, try modifying your post_params method to look like this:

    def post_params
      params.permit(:topic_id)
      params.require(:post).permit(:text, :char => [:id]).merge({ip: request.remote_ip, user: current_user})
    end
    

    The big difference being we pass merge a hash, but also specify what we want from :char which is nested inside :post, as well as whitelisting :topic_id (which is not a part of :post).