【问题标题】:Displaying Triple Nested Form Values (Rails 5.1)显示三重嵌套表单值 (Rails 5.1)
【发布时间】:2018-02-09 07:55:01
【问题描述】:

我在研究如何在三重嵌套资源中显示最深的值时遇到问题。 (RoastCountryRegion)。

我可以通过以下方式显示第一级:

<% for countries in @roast.countries %>
   <br />
  <strong>Country:</strong>
  <%= countries.country_name %>
<% end %>

但我无法显示下一个级别。

#&lt;Country::ActiveRecord_Associations_CollectionProxy:0x007fbaf8ad25b8&gt; 的未定义方法“区域”

我尝试了以下方法,但这不起作用。

<% for regions in @roast.countries.regions %>
   <br />
  <strong>Country:</strong>
  <%= countries.regions.region_name %>
<% end %>

@roast.country.regions 也不起作用。

【问题讨论】:

    标签: ruby-on-rails nested nested-loops nested-forms


    【解决方案1】:

    大致上应该是这样的:

    <% @roast.countries.each do |country| %>
      <div class='country-container'>
        <div class='country title'>
          Country:
        </div>
        <div class='country-name'>
          <%= country.country_name %>
        </div>
        <div class='regions-container'>
          <div class='region title'>
            Regions:
          </div>
          <div class='regions'>
            <% country.regions.each do |region| %>
              <div class='region'>
                <%= region.name %>
              </div>
            <% end %>  
          </div>
        </div>
      </div>
    <% end %>
    

    所有divsclasses 都可以让您在css 中进行样式设置,而不必使用&lt;strong&gt; 等。例如,如果您使用sass,则可以执行以下操作:

    .country-container
      .title
        font-weight: bold
        font-size: 110%
        &.country 
          color: red 
        &.region
          color: blue
      .regions-container
        .region
          color: green
    

    这个:

    <% for countries in @roast.countries %>
    

    不是很红宝石风格。你应该改用each

    【讨论】:

      【解决方案2】:

      如果您只有一个国家,并且需要获取该国家/地区的所有区域,则循环如下所示

      <% for region in @roast.countries.first.regions %>
          <br />
          <strong>Region:</strong>
          <%= region.region_name %>
      <% end %>
      

      或者,如果您需要显示所有国家/地区的所有地区,则循环如下所示

      我猜模型关系看起来像

      class Roast < ApplicationRecord
          has_many :countries
      end
      
      class Country < ApplicationRecord
          belongs_to :roast
          has_many :regions
      end
      
      class Region < ApplicationRecord
          belongs_to :country
      end
      

      然后

      <% for country in @roast.countries %>
          <br />
          <strong>Country:</strong>
          <%= country.country_name %>
      
          <% for region in country.regions %>
              <br />
              <strong>Region:</strong>
              <%= region.region_name %>
          <% end %>
      <% end %>
      

      最好是ruby each 方法而不是loop,如下所示

      <% @roast.countries.each do |country|  %>
          <br />
          <strong>Country:</strong>
          <%= country.country_name %>
      
          <% country.regions.each do |region|  %>
              <br />
              <strong>Region:</strong>
              <%= region.region_name %>
          <% end %>
      <% end %>
      

      【讨论】:

      • 是的,每种方法都能完美运行@fool-dev。谢谢。
      【解决方案3】:
      <% for country in @roast.countries %>
        <br />
        <strong>Country:</strong>
        <%= country.country_name %>
      
        <br/>
        <p>Regions:</p>
        <% for region in country.regions %>
          <%= region.region_name %>
        <% end %>
      <% end %>
      

      您可以为每个国家/地区选择其地区

      【讨论】:

        猜你喜欢
        • 2017-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多