Undefined method? Controller params in Rails 4 -
Undefined method? Controller params in Rails 4 -
i getting weirdest error have ever seen, consider next create method:
def create post = post.find_by(id: params[:post_id]) @comment = comment.new(comment_create_params) @comment.post_id = post.id #i know line useless have yet refactor. controller_save(@comment) end
from here have comment_create params
private method defined such:
def comment_create_params params.require(:comment).permit(:author, :comment, :parent_id) end
now consider next params
passed in:
params => {"author"=>"157685iyutrewe1wq", "comment"=>"14253647turyerwe", "action"=>"create", "controller"=>"api/v1/comments", "post_id"=>"126"}
based on looks correct. running through function should save. till next error:
nomethoderror: undefined method `permit' "14253647turyerwe":string
i have no thought means - think trying treat: "14253647turyerwe"
method string? not sure....
params
params.require(:comment).permit(:author, :comment, :parent_id)
this hash inherits comment
key, this:
{"comment" => { "id" => "5", "name" => "test" } }
so when utilize require
method, you're saying "we need top-level hash key", rails go nested hash & utilize permit
method locate other attributes, shown above.
the problem have this:
params => {"author"=>"157685iyutrewe1wq", "comment"=>"14253647turyerwe", "action"=>"create", "controller"=>"api/v1/comments", "post_id"=>"126"}
the problem here you're calling require
on comment
key; string. prepare this, you'll need this:
def comment_params params.permit(:author, :comment, :action) end
--
save
something else need consider controller_save
method. i've never seen before, , against convention. not problem this, means if team members on app, or want upgrade rails, pain adapt it.
i utilize standard .save
method, this:
#app/controllers/comments_controller.rb def create ... @comment.save end
ruby-on-rails
Comments
Post a Comment