【问题标题】:Ruby on Rails form.object does not respond to object methods: undefined method for nil:NilClassRuby on Rails form.object 不响应对象方法:nil:NilClass 的未定义方法
【发布时间】:2012-08-06 15:09:35
【问题描述】:

我正在为 Rails 3 使用 ryanb 的 nested_form gem,并试图访问在表单中传递的对象,以便我可以标记表单生成器生成的字段。

我有以下基本结构:

Trainer has_many :clients, through: :relationships
Trainer has_many :schedules

Client has_many :trainers, through: :relationships
Client has_many :schedules

我的 Client#edit 页面的查看代码:

<% provide(:title, @client.name) %>
<h2><%= link_to @client.name, @client %></h2>

<%= render :partial => 'shared/errors', :locals => { :user => @client } %>

<%= nested_form_for @client do |f| %>

<p><%= f.link_to_add "Add Schedule", :schedules %></p>

<%= f.fields_for :schedules do |schedule_form| %>

        <%= schedule_form.object.trainer.name %>

    <%= schedule_form.date_select :scheduled_date %> 
    <%= schedule_form.link_to_remove "Remove" %>

    <%= schedule_form.hidden_field :trainer_id %>
    <%= hidden_field_tag :trainer_id %>

    <% end %>

<br><%= f.submit "Create", class: 'btn btn-large btn-primary' %></br>
<% end %>

&lt;%= schedule_form.object.trainer.name %&gt; 行会引发 undefined method 'name' for nil:NilClass 错误。

这就是奇怪的地方。我将在下面发布几个场景,我修改该行,然后在页面上发布输出:

<%= schedule_form.object.trainer %>
<Trainer:0x007fdcedf3f800>

<%= schedule_form.object.trainer.class %>
Trainer

<%= schedule_form.object.trainer.is_a?(Object) %>
true

<%= schedule_form.object.trainer.respond_to?(:name) %>
true

<%= schedule_form.object.trainer.name %>
undefined method 'name' for nil:NilClass

<%= t_id = schedule_form.object.trainer_id %>
1
<%= Trainer.find(t_id) %>

<%= t_id = schedule_form.object.trainer_id %>
1
<%= Trainer.find_by_id(t_id) %>
#<Trainer:0x007fdcedf09660>

<%= t_id = schedule_form.object.trainer_id %>
1
<%= Trainer.find_by_id(t_id).name %>
undefined method 'name' for nil:NilClass

所以基本上 - 我可以使用表单构建器来检索 :schedule 对象并可以查询它来检索 :trainer 对象(schedule.trainerschedule.trainer_id);然而,:trainer 对象不会响应它的任何实例方法,尽管据我所知,它是 Trainer 类的对象。

如何返回trainer.name?这就是我想要的:一个标签。请帮忙。

编辑:添加培训师模型

class Trainer < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password
  has_many :relationships
  has_many :clients, through: :relationships
  has_many :schedules

  validates :name, presence: true
  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 }
  validates :password, presence: true, length: { minimum: 6 }, on: :create
  validates :password_confirmation, presence: true, length: { minimum: 6 }, on: :create

  before_save { |trainer| trainer.email = email.downcase } 

end

编辑 - 稍后添加。

我尝试使用以下代码,它按预期工作:

<%= t_id = 1 %>
<%= Trainer.find(t_id).name %>

返回

Joe Trainer

但是:

<%= t_id = schedule_form.object.trainer_id %>
  > 1
<%= Trainer.find(t_id) %>
  > Couldn't find Trainer without an ID
<%= Trainer.find_by_id(t_id) %>
  > #<Trainer:0x007fea86b9a168>
<%= Trainer.find_by_id(t_id).name %>
  > undefined method 'name' for nil:NilClass

显然,从 schedule_form.object 返回的任何东西都有一些“额外的包袱”。有没有办法获取t_id = schedule_form.object.trainer_id 并对其进行清理,以便我拥有的只是一个纯整数,不受任何先前历史的影响?

【问题讨论】:

  • 你试过 我承认这是奇怪的行为。
  • 我看到的另一件事很奇怪,你的 has_many :through 关系没有按照我的预期建立。我希望看到它设置为客户端 has_many :trainers, :though => :schedules。现在是 :through => :relationships。你在某处有关系模型吗?
  • 看起来很奇怪。您能否提供培训师课程的代码或完整的堆栈跟踪?
  • @joeyjoejoejr - 有一个关系模型;它基本上只是一个连接表,其中有一列用于:client_id,一列用于:trainer_id。我确实尝试过 它返回 false。
  • @RishavRastogi - 我添加了 Trainer 模型。

标签: ruby-on-rails ruby class methods


【解决方案1】:

您的代码在 Schedule 对象上失败,但您没有发布该模型。

很可能它崩溃是因为您没有使用@trainer.schedules.create 创建计划,而是使用了Schedule.create 或Schedule.new...然后trainer 属性未初始化,因此为nil。

【讨论】:

  • 我正在使用 client.update_attributes 来更新所有时间表。即使对于已经保存到数据库的对象,trainer 属性也会返回 nil。但是, schedule_form.object.trainer_id 返回正确的 id - 你知道为什么我不能根据它搜索 Trainer 吗? Trainer.find 不应依赖于正在初始化的时间表。
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
相关资源
最近更新 更多