【问题标题】:Error when I implement ruby on rails with refinery当我在炼油厂的轨道上实现 ruby​​ 时出错
【发布时间】:2014-05-21 02:29:40
【问题描述】:

我正在尝试用炼油厂实现 ruby​​ on rails 入门的博客,当我想写评论时,它说:

NoMethodError in CommentsController#create

undefined method `permit' for {"commenter"=>"lkjlkm", "body"=>",knm,m"}:ActiveSupport::HashWithIndifferentAccess
Rails.root: C:/Sites/blog2

Application Trace | Framework Trace | Full Trace
app/controllers/comments_controller.rb:8:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"rh9Kq6QxfXuZxlM9nFvsAjxiA5UnueEFKJ3ypcKy3xQ=",
 "comment"=>{"commenter"=>"lkjlkm",
 "body"=>",
knm,
m"},
 "commit"=>"Create Comment",
 "post_id"=>"5"}
Show session dump

Show env dump

Response

Headers:

None

我的 cmets_controller 代码是:

 class CommentsController < ApplicationController

http_basic_authenticate_with name: "antonio", password: "opening", only: :destroy

def create
@post = Post.find(params[:post_id])

@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end

def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end

end

我希望你的答案很快 谢谢大家

【问题讨论】:

    标签: ruby-on-rails ruby html ruby-on-rails-3 refinery


    【解决方案1】:

    最好的办法就是把它定义成这样的私有方法

    @comment = @post.comments.create(comment_params)
    
    private
    
    def comment_params
    
       params.require(:comment).permit(:commenter, :body)
    
    end
    

    【讨论】:

    • 非常感谢,但现在我还有其他错误:TypeError in CommentsController#create can't convert Symbol into String
    • @user3510761 错误是指@comment = @post.comments.create(params.require(:comment).permit(:commenter, :body))这一行吗?
    • @user3510761 可能是假码。试试private方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多