Rails显示表数据

Geo*_*cks 1 ruby-on-rails view

我开发了三个类的rails应用程序.

class Location < ActiveRecord::Base
    has_many :observations

end

class Observation < ActiveRecord::Base
  belongs_to :location
  has_one :rainfall
  has_one :temperature
  has_one :winddirection
  has_one :windspeed
  has_one :dewpoint

end


class Rainfall < ActiveRecord::Base
  belongs_to :observation

end
Run Code Online (Sandbox Code Playgroud)

Railfall通过observation_id与Observation相关,而Observation通过location_id与Location相关.

如果我去控制台并键入类似:Location.all.first.observations.first.rainfall.value的内容,它会返回降雨量值列中的数据

但是,当我想在表格和我的视图/ location/index.html.erb中组合rails中的所有信息时,我将:

<tbody>
    <% @locations.each do |location| %>
      <tr>

        <td><%= location.name %></td>



        <% unless @observations = nil %>

        <% location.observations.each do |obs| %>
        <td><%= obs.name %></td>

        <% unless @rainfalls = nil %>
        <td><%= obs.rainfalls.value %></td>


    <% end %>
    <% end %>
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

    undefined method `rainfalls' for Observation:0x00000004219068>
Run Code Online (Sandbox Code Playgroud)

我花了最近5个小时尝试了我能做的一切,并且让我感到沮丧....任何想法?

注意我有一个位置控制器,但观察和降雨是作为模型生成的,所以不要.我认为我需要向我的位置控制器添加一些东西,只是无法解决问题,我不确定为什么它在控制台中返回正确的数据,只是没有在应用程序中.

Mar*_*pka 5

Observation.has_one :rainfall,所以它应该是:

<%= obs.rainfall.value %>
Run Code Online (Sandbox Code Playgroud)