Ruby on rails Rails应用程序中未定义的方法
我是Rails的新手,我正在学习一个在线教程,该教程让你构建了一个书评应用程序。除了我提交评论外,一切似乎都正常。当我这样做时,我会得到以下错误:Ruby on rails Rails应用程序中未定义的方法,ruby-on-rails,Ruby On Rails,我是Rails的新手,我正在学习一个在线教程,该教程让你构建了一个书评应用程序。除了我提交评论外,一切似乎都正常。当我这样做时,我会得到以下错误: undefined method `book_id=' for nil:NilClass 这是我的管理员: class ReviewsController < ApplicationController before_action :find_book def new @review = Review.new end def c
undefined method `book_id=' for nil:NilClass
这是我的管理员:
class ReviewsController < ApplicationController
before_action :find_book
def new
@review = Review.new
end
def create
@reivew = Review.new(review_params)
@review.book_id = @book.id
@review.user_id = @current_user.id
if @review.save
redirect_to book_path(@book)
else
render "new"
end
end
private
def review_params
params.require(:review).permit(:rating, :comment)
end
def find_book
@book = Book.find(params[:book_id])
end
end
class-ReviewsController
以下是我的审查模式:
class Review < ApplicationRecord
belongs_to :book
belongs_to :user
end
class Review
这是我的图书模型:
class Book < ApplicationRecord
belongs_to :user
belongs_to :category
has_many :reviews
has_attached_file :book_img, styles: { book_index: "250x350>", book_show: "325x475>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :book_img, content_type: /\Aimage\/.*\z/
end
classbook”,book\u show:“325x475>”},默认url:“/images/:style/missing.png”
验证\附件\内容\类型:图书\图片,内容\类型:/\Aimage\/.\z/
结束
我觉得在过去的两个小时里我一直在阅读帮助论坛。我卡住了。任何帮助都将不胜感激 您的
创建操作中的查看中有一个输入错误
更改以下行
@reivew = Review.new(review_params)
到
出现错误的原因是@review
是nil
,并且您不能在nil
对象上调用方法book\u id
。谢谢!它现在有一个问题,这一行:@review.user\u id=@current\u user.id它说:nil的未定义方法'id':nilclasists,因为@current\u user
是nil。在哪里设置@current\u user
变量?如果您使用的是Deviate,它应该是当前用户
。顺便说一句,如果我的答案对你有帮助,请投票并接受我的答案(超过时限)。
@review = Review.new(review_params)