ruby-on-rails - # 未定义的方法“错误”
所以我收到以下错误:
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 来处理。
我在自己的网站上所做的是有一个没有动画、标准颜色等的标准样式表。然后是一个单独的样式表:
- 动画(默认包含)
- 颜色(不使用 !important 覆盖标准颜色)
- 没有 JavaScript - 因为这对于某些东西和屏幕阅读器来说可能是一个真正的问题(我隐藏了某些在没有启用 javascript 的情况下会导致混乱的元素)
这是由一个单独的设置页面增强的,用户可以在其中选择字体大小、颜色、字体系列、动画与否、图像或不等。存储在本地存储中,然后在每个页面加载时处理成一些可以使用的简单类用于对项目进行细粒度控制。
解决方案
问题
- 您不能访问
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 %>
推荐阅读
- python - 在 Python 3 中读取 cookie 值
- mysql - MySQL 中 DELETE 之后的术语
- spring - Spring REST Docs 使用静态配置类设置 MockMvc
- android - Android Inside 自定义对话框:- 如何打开 DatePickerDialog?
- ios - 带有 CollectionView 和文本行的 TableView 部分
- php - findAll() 允许的内存大小 Symfony 4
- node.js - 如何使用赛普拉斯测试登录到 GSuite 日历
- spring-boot - Flux 完成时的返回值?
- apache - php imap_open 在代理 ntlm auth 后面
- amazon-s3 - 如何使用 IAM 服务动态创建 Airflow S3 连接
所以我收到以下错误:
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 来处理。
我在自己的网站上所做的是有一个没有动画、标准颜色等的标准样式表。然后是一个单独的样式表:
- 动画(默认包含)
- 颜色(不使用 !important 覆盖标准颜色)
- 没有 JavaScript - 因为这对于某些东西和屏幕阅读器来说可能是一个真正的问题(我隐藏了某些在没有启用 javascript 的情况下会导致混乱的元素)
这是由一个单独的设置页面增强的,用户可以在其中选择字体大小、颜色、字体系列、动画与否、图像或不等。存储在本地存储中,然后在每个页面加载时处理成一些可以使用的简单类用于对项目进行细粒度控制。
解决方案
问题
- 您不能访问
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 %>
推荐阅读
- python - 在 Python 3 中读取 cookie 值
- mysql - MySQL 中 DELETE 之后的术语
- spring - Spring REST Docs 使用静态配置类设置 MockMvc
- android - Android Inside 自定义对话框:- 如何打开 DatePickerDialog?
- ios - 带有 CollectionView 和文本行的 TableView 部分
- php - findAll() 允许的内存大小 Symfony 4
- node.js - 如何使用赛普拉斯测试登录到 GSuite 日历
- spring-boot - Flux 完成时的返回值?
- apache - php imap_open 在代理 ntlm auth 后面
- amazon-s3 - 如何使用 IAM 服务动态创建 Airflow S3 连接