【问题标题】:Rails Issue - Displaying Errors During ValidationRails 问题 - 在验证期间显示错误
【发布时间】:2013-09-05 22:20:44
【问题描述】:

在我的 Rails 4 应用程序中,我有两个模型 - usersaccommodationsUsers有很多accommodations,并且已经在相关模型中设置(即belongs_tohas_many等)。我正在尝试验证添加到我的住宿模型中的数据并显示错误(如果有)。

当我尝试访问 accommodations/new 操作时,我收到以下错误:

NoMethodError in Accommodations#new
undefined method `errors' for nil:NilClass

当我的用户尝试注册时,我可以使用此功能,代码如下:

users/new.html.erb:

<% if @user.errors.any? %>
    <% @user.errors.full_messages.each do |msg| %>
         <p class="error"><%= msg %></p>
    <% end %>
<% end %>

但是当我尝试对住宿做同样的事情时,我得到了提到的错误:

accommodations/new.html.erb:

<% if @accommodation.errors.any? %>
    <% @accommodation.errors.full_messages.each do |msg| %>
         <p class="error"><%= msg %></p>
    <% end %>
<% end %>

阅读后我意识到第一个在UsersController:中起作用

def new
  @user = User.new
end

但在我的 AccommodationsController #new 操作中尝试以下操作后,它仍然无法正常工作

@accommodation = Accommodation.new

我的Accommodation模型如下:

class Accommodation < ActiveRecord::Base
  belongs_to :user

  validates :user_id, presence: true
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

end

【问题讨论】:

  • 发布您的 index.html.erb
  • 分享您的住宿控制器索引操作、模型和完整的错误消息。
  • 我的住宿索引操作中没有任何内容,因为我不知道该放什么!我已经用我的住宿模型和额外的错误消息行编辑了帖子
  • @tommyd456 你看到我的答案了吗?
  • 在 has_one 关联中,该方法被build_accommodation取代

标签: ruby-on-rails validation ruby-on-rails-4 activemodel


【解决方案1】:

在控制器的new 方法中,您应该使用current_user.accommodations.build 而不是accommodation.new,因为您想要一个新的用户住宿(住宿belong_to user)。当我们有这种类型的关系时,请记住 .new 改为 .build

你有这样的用户和住宿之间的关系:

class User < ActiveRecord::Base

   has_many :accommodations

end

和:

class Accommodation < ActiveRecord::Base

   belongs_to :user

end

你应该有一个像这样的新方法:

class AccommodationsController < ApplicationControlle
   def new
      @accommodation = current_user.accommodations.build
   end
end

注意:这里 current_user 是已登录的用户

然后在您的create 方法中,我认为如果发生错误,您会渲染new,否则如果创建成功,您会重定向到accommodations#index 页面?

在这种情况下,您应该向住宿控制器添加索引操作:

class AccommodationsController < ApplicationControlle

    def index
       # add here what do you need to show, i think you want to show all accomodations, so just add the following line (you can also add pagination if you want later) :
       @accommodations = Accommodation.all
    end

    .
    .
end

最后你应该有一个 index view page : views/accommodation/index.html.erb 循环 @accommodations 中的每个元素并显示你需要什么作为信息......

【讨论】:

  • 感谢您的详细回复 - 我已经稍微编辑了原始问题以澄清,但是当我添加 ruby​​ 以显示任何错误作为验证规则的结果时 - 这是 NoMethodError 发生的时间
  • 是的,我已经看到了,谢谢!恐怕不行 - 一旦我添加 ruby​​ 代码以显示验证错误,我的错误就会发生(旁注:对于任何复制和粘贴的人来说,住宿都用两个 m 拼写)。
  • 还有——错误不是我尝试提交表单时——而是我尝试加载住宿/新的时候
  • 更改 [at]accommodation = Accommodation.new 与 [at]accommodation = current_user.accommodations.build ,您是否已经有一个返回当前用户(已连接)的方法
  • 是的,这就是我所做的 - 我在住宿控制器中添加了新操作,但不起作用。但它必须与这一行有关,因为用户控制器一个工作(@user = User.new) - 可能与关系有关
猜你喜欢
  • 2011-05-16
  • 2011-03-27
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
相关资源
最近更新 更多