所以我收到以下错误:

undefined method `errors' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x00007f1a3e2ecf48>

我可以获得评论计数和其他类似的东西,但是,我无法显示任,ruby-on-rails,ruby-on-rails-5,rails-activerecord"/>

首页 > 解决方案 > # 未定义的方法“错误”

所以我收到以下错误:

undefined method `errors' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x00007f1a3e2ecf48>

我可以获得评论计数和其他类似的东西,但是,我无法显示任

问题描述

所以我收到以下错误:

undefined method `errors' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x00007f1a3e2ecf48>

我可以获得评论计数和其他类似的东西,但是,我无法显示任何验证错误。

这是我的代码

_new_comment

<% if signed_in? %>
  <div class="row">
    <%= form_with(model: [@product, @product.comments.build], local: true) do |f| %>
      <% if @product.comments.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@product.comments.errors.count, "error") %> prohibited this comment from being saved:</h2>

          <ul>
          <% @product.comments.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

comments_controller

class CommentsController < ApplicationController
  def create
    @product = Product.find(params[:product_id])
    @comment = @product.comments.new(comment_params)
    @comment.user = current_user
    @comment.save

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @product, notice: 'Review was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { redirect_to @product, alert: 'Review was not saved successfully.' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
  end

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

任何帮助在这里表示赞赏。


这里有很多好的答案,以为我会投入两分钱。

两页是可以接受的,但一个坏主意,可维护性,搜索引擎优化等都是已经提到的因素。

但是没有提到的是简单地具有不同的样式表以实现可访问性。

如果您的 html 标记是有效的并且具有逻辑结构,那么为了使页面可访问,这是添加 aria 属性的“简单”案例——它涵盖了具有屏幕阅读器的用户。

您说这是一个复杂的页面,因此为残疾用户设计的“限制”可能会使您在此过程中简化和改进组件,从而使客户受益。将您的 HTML 更改为可访问不仅有利于您的残疾用户,而且您会发现它使网站更容易为身体健全的用户使用,所以这是一个加号。

一旦您的 HTML 有效并针对屏幕阅读器的可访问性进行了调整,则可使用不同的样式表(或针对不同场景的多个样式表)轻松实现可访问性的其他部分(对于部分视力、色盲等)。

残疾用户需要许多不同的东西,但重点应该是网站可以在 1920 * 1080 屏幕上以 400% 缩放使用,可以更改颜色,可以更改字体,并且可以为认知障碍的用户禁用动画。

这一切都可以用 CSS 来处理。

我在自己的网站上所做的是有一个没有动画、标准颜色等的标准样式表。然后是一个单独的样式表:

  1. 动画(默认包含)
  2. 颜色(不使用 !important 覆盖标准颜色)
  3. 没有 JavaScript - 因为这对于某些东西和屏幕阅读器来说可能是一个真正的问题(我隐藏了某些在没有启用 javascript 的情况下会导致混乱的元素)

这是由一个单独的设置页面增强的,用户可以在其中选择字体大小、颜色、字体系列、动画与否、图像或不等。存储在本地存储中,然后在每个页面加载时处理成一些可以使用的简单类用于对项目进行细粒度控制。

标签: ruby-on-railsruby-on-rails-5rails-activerecord

解决方案


问题

  • 您不能访问errors一个ActiveRecord::Relation对象,而是访问单个Comment对象。
  • 在您的控制器操作create中,您redirect_to(@product)无法保存评论,这将忽略已构建的对象并在呈现评论表单时@comment构建一个新对象(通过)。form_with(model: [@product, @product.comments.build])相反,您只需要render具有已构建@comment对象的产品页面。
  • 你有@comment.save两次create行动。

解决方案

products_controller.rb

def show
  # your current logic
  @comment = @product.comments.build
  # render
end

评论控制器.rb

class CommentsController < ApplicationController
  def create
    @product = Product.find(params[:product_id])
    @comment = @product.comments.new(comment_params)
    @comment.user = current_user

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @product, notice: 'Review was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render 'products/show', alert: 'Review was not saved successfully.' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:user_id, :body, :rating)
  end
end

_new_comment.html.erb

<%= form_with(model: [@product, @comment], local: true) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
        <% @comment.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
<% end %>

推荐阅读