【问题标题】:how to edit comment within article's page - keep getting error about nil如何在文章页面中编辑评论 - 不断收到关于 nil 的错误
【发布时间】:2017-10-30 07:52:55
【问题描述】:

我有典型的文章和评论博客格式。是典型的has_many/belongs_to,即Guide的博客类型。

但是,我正在尝试编辑文章中的评论,但我对为此构建正确的表单一无所知。

它也是局部的,这对我来说更复杂。

任何帮助和/或教育我将不胜感激。

评论的模型

class Comment < ApplicationRecord
  belongs_to :article
end

文章的模型

class Article < ApplicationRecord

    has_many :comments

    validates :title, presence: true, length: { minimum: 5} 

end

文章的显示页面

<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>

<h2>Comments</h2>
<%= render @article.comments %>

<h2>Add a comment:</h2>
<%= render 'comments/form' %>

<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>

评论的 _comment.html.erb 页面

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>


<p>
  <%= link_to 'Destroy Comment', [comment.article, comment],
               method: :delete,
               data: { confirm: 'Are you sure?' } %>
</p>

<p>
    <%= link_to 'Edit', edit_article_comment_path(@article, comment) %>
</p>

评论的_form.html

<%= form_for([@article, @comment]) do |f| %>
  <p>
    <%= f.label :commenter %><br>
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

错误

它来自评论的_form.html.erb页面引用此行:&lt;%= form_for([@article, @comment]) do |f| %&gt;...错误是:First argument in form cannot contain nil or be empty...

文章管理员

class ArticlesController < ApplicationController


    def index
        @articles = Article.all 
    end


    def new
        @article = Article.new
    end

    def edit 
        @article = Article.find(params[:id])
    end

    def create
        #render plain: params[:article].inspect
        #@article = Article.new(params[:article])
        #@article = Article.new(params.require(:article).permit(:title, :text))
        @article = Article.new(article_params)

        if @article.save 
            redirect_to @article 
        else
            render 'new'
        end
    end

    def update 
        @article = Article.find(params[:id])

        if @article.update(article_params)
            redirect_to @article 
        else
            render 'edit'
        end
    end


    def show 
        @article = Article.find(params[:id])
    end

    def destroy 
        @article = Article.find(params[:id]) 
        @article.destroy 

        redirect_to articles_path 
    end


    private 
        def article_params
            params.require(:article).permit(:title, :text)
        end

end

评论控制器

class CommentsController < ApplicationController
  def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)
    redirect_to article_path(@article)
  end

  def destroy 
    @article = Article.find(params[:article_id])
    @comment = @article.comments.find(params[:id])
    @comment.destroy 
    redirect_to article_path(@article) 
  end

  private
    def comment_params
      params.require(:comment).permit(:commenter, :body)
    end
end

【问题讨论】:

  • 请在您设置@article@comment 的位置发布控制器操作。
  • 另外,如何渲染部分内容?
  • 显示你的评论控制器
  • @Gerry,对这么晚的回复感到遗憾。已发布ArticlesComments 控制器。
  • @Pavan,不知道你的意思是我如何渲染部分......认为它在文章的显示页面中可见?即&lt;%= render @article.comments %&gt;&lt;%= render 'comments/form' %&gt; ...抱歉回复晚了。

标签: ruby-on-rails


【解决方案1】:

表单中的第一个参数不能包含 nil 或为空

错误是由于表单中的@article@comment 为nil。由于您正在使用部分呈现表单,因此您还需要将变量发送到部分

&lt;%= render 'comments/form' %&gt; 更改为&lt;%= render 'comments/form', article: @article, comment: @comment %&gt;

并在表单中有这个&lt;%= form_for([article, comment]) do |f| %&gt;

同样在articles控制器show方法中,你应该定义@comment

def show 
  @article = Article.find(params[:id])
  @comment = Comment.new
end

【讨论】:

  • @Paven,这行得通,谢谢。想知道@comment = Comment.new 是否是反模式?另外,尽管这解决了这个问题,但这个问题的整个前提最初是与编辑带来相同错误的评论有关,即First argument in form cannot contain nil or be empty ...因此无法编辑评论...最好再次将其作为单独的问题发布?
  • @user273072545345 如果需要,您可以将其作为另一个问题发布。同样适用于编辑。唯一的区别是@comment 不会是一个新实例。它将保存这样的记录(即特定评论)@comment = Comment.find(params[:id])
  • 另外@comment = Comment.new在这里不是反模式,因为表单是添加新评论,@comment应该是评论的新实例。希望你明白了。我将不胜感激并标记为正确答案。很高兴为您提供帮助!
  • @Paven,很抱歉延迟接受您的回答。自从你解决了这个问题后,无论如何都会这样做。明天将审查并将其作为单独的问题发布。谢谢!
  • @Paven,也想知道为什么有必要这样做&lt;%= render 'comments/form', article: @article, comment: @comment %&gt;,因为_comment.html.erb 表单有这个&lt;%= form_for([@article, @comment]) do |f| %&gt; ...
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 2021-12-24
  • 2017-10-15
  • 1970-01-01
相关资源
最近更新 更多