我正在使用Jumpstartlab博客教程。当我运行服务器而我想要打开文章时,我收到以下错误:
ArgumentError in Articles#show
Showing /home/darek/rails_projects/blogger/app/views/comments/_form.html.erb where line
#3 raised:
First argument in form cannot contain nil or be empty
Extracted source (around line #3):
1 <h3>Post a Comment</h3>
2
3 <% form_for [ @article, @comment ] do |f| %>
4 <p>
5 <%= f.label :author_name %><br/>
6 <%= f.text_field :author_name %>
_form.html.erb
<h3>Post a Comment</h3>
<% form_for [ @article, @comment ] do |f| %>
<p>
<%= f.label :author_name %><br/>
<%= f.text_field :author_name %>
</p>
<p>
<% f.label :body %><br/>
<% f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
comments_controller.rb
class CommentsController < ApplicationController
def create
article_id = params[:comment].delete(:article_id)
@comment = Comment.new(params[:comment])
@comment.article_id = article_id
@comment.save
redirect_to article_path(@comment.article)
end
end
我已经尝试将代码与github存储库的教程进行比较,但它并没有帮助。本教程是为Rails 3.x准备的,我正在研究4.0 有什么想法吗?
答案 0 :(得分:1)
该错误消息表明您在form_for
中使用的对象为零或空,即您尚未定义它们。由于您尚未发布show
行动,请尝试添加以下内容(假设您已设置article
和comment
之间的关系):
# ArticlesController
def show
@article = Article.find(params[:id]) # However you are retrieving your @article
@comment = @article.comments.build
end
答案 1 :(得分:0)
试试这个:
def create
article_id = params[:comment].delete(:article_id)
@article = Article.find(article_id)
@comment = @article.comments.build(params[:comment])
@comment.save
redirect_to article_path(@comment.article)
end