0

我正在使用 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 %>  

评论控制器.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 有什么想法吗?

4

2 回答 2

1

该错误消息表明您在 中使用的对象form_for是 nil 或空的,即您尚未定义它们。由于您尚未发布您的show操作,请尝试添加以下内容(假设您之间存在关系article并且comment已经设置):

# ArticlesController

def show 
  @article = Article.find(params[:id]) # However you are retrieving your @article  
  @comment = @article.comments.build
end 
于 2013-07-24T14:37:50.827 回答
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
于 2013-07-24T14:25:47.920 回答