【发布时间】:2014-03-18 21:24:47
【问题描述】:
我正在尝试创建一个显示多个一对多关联的视图。在我的应用程序中,每个“检查”都分配给一个“客户”和“代理”。我已经建立了我的关系。代理正在按应有的方式返回,但是客户端正在为“名称”返回一个未定义的方法 nil:nilClass。
# inspections_controller.rb
def new
@inspection = Inspection.new
@agents = Agent.all
@clients = Client.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @inspection }
end
end
# inspection.rb (model)
belongs_to :agent
belongs_to :client
# agent.rb (model)
has_many :inspections
# client.rb (model)
has_many :inspections
# index.html.erb (inspections view)
<td><%= inspection.agent.name %></td> # works
<td><%= inspection.client.name %></td> # returns undefined method `name'
我在这里不知所措,因为关联和表格的设置完全相同。我可以返回<%= inspection.client_id %> 就好了。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 relationship