【问题标题】:Rails loop arrangement轨道环布置
【发布时间】:2013-09-12 07:06:55
【问题描述】:

所以我有一些循环正在进行,但我试图组织信息的显示方式。

我正在为样式表使用引导程序。

这是循环:

<% @users.each do |i| %>
        <h3><%= i.firstname %><%= i.lastname %></h3>
        Birthday: <%= i.dateofbirth %><br />
        Address: <%= i.address %><br />
        City: <%= i.city %><br />
        Province: <%= i.province %><br />
        Phone Number: <%= i.phonenumber %><br />
        Service Provider: <%= i.serviceprovider %><br />
        Gender: <%= i.gender %><br />
        Languages: <%= i.languages %><br />
  <% end %>



  <% @apps.each do |f| %>

        <h3>School Information</h3>
        Highschool: <%= f.highschool %><br />
        Address: <%= f.highschool_address %><br />
        City: <%= f.highschool_city %><br />
        Province: <%= f.highschool_province %><br />
        Postal Code: <%= f.highschool_postalcode %><br />



        <h4>Post Secondary Schools of Interest</h4>
        <% f.postsecondaries.each do |ps| %>
        Post Secondary Name: <%= ps.postsecondary %><br />
        Address: <%= ps.postsecondary_address %><br />
        City: <%= ps.postsecondary_city %><br />
        Province: <%= ps.postsecondary_province %><br />
        Postal Code: <%= ps.postsecondary_postalcode %><br />
        Country: <%= ps.postsecondary_country %><br />
        Program: <%= ps.postsecondary_program %><br />
        Faculty: <%= ps.postsecondary_faculty %><br />
        Status: <%= ps.postsecondary_status %><br />
        <% end %>




        <h3>Grades</h3>
        <% f.grades.each do |grade| %>
        <br />Course: <%= grade.course %><br />
        Grade: <%= grade.course_grade %>
        <% end %>

        <h3>Extracurricular Activities</h3>
        <% f.extra_activities.each do |e| %>
        Activity: <%= e.activity %><br />
        Position: <%= e.activity_position %><br />
        Dates: <%= e.activity_dates %><br />
        Times Per Week: <%= e.activity_timeperweek %><br />
        Contact Name: <%= e.activity_contact %><br />
        Contact Position: <%= e.activity_contact_position %><br />
        Contact Phone Number: <%= e.activity_contact_phonenumber %><br />
        Contact Email: <%= e.activity_contact_email %><br />
        Description: <%= e.activity_description %>
        <% end %>

        <h3>Paragraph</h3>
        Recall a time... <%= f.recall_a_time %><br />
        Description: <%= f.paragraph_description %>
        <br />Recall a time... <%= f.recall_a_time_two %><br />
        Description: <%= f.paragraph_description_two %>
        <br />Recall a time... <%= f.recall_a_time_three %><br />
        Description: <%= f.paragraph_description_three %>
  <% end %>

我无法将学校信息发布在存储用户信息的同一引导程序中。

【问题讨论】:

  • 用户井和学校信息井之间的断层是什么样的?可以再发一张图吗?另外,你的UserApp 模型之间是什么关系?

标签: css ruby-on-rails twitter-bootstrap


【解决方案1】:

由于User 可能有许多应用程序,因此您需要在遍历每个用户时遍历每个用户的应用程序。这样的事情可能会奏效:

<% @users.each do |user| %>
    <!-- iterate through user attributes -->

    <% user.apps.each do |app| %>
        <!-- iterate through app attributes -->
    <% end

<% end %>

更新

如果User 只有一个App,则无需遍历每个用户的应用程序,因为只有一个:

<% @users.each do |user| %>
    <!-- iterate through user attributes -->

    <h3>School Information</h3>
    Highschool: <%= user.app.highschool %><br />
    Address: <%= user.app.highschool_address %><br />
    City: <%= user.app.highschool_city %><br />
    Province: <%= user.app.highschool_province %><br />
    Postal Code: <%= user.app.highschool_postalcode %><br />

<% end %>

【讨论】:

    【解决方案2】:

    我建议为每个循环内容创建部分内容

    类似的东西

    <% @users.each do |i| %>
        <%= render 'users' , locals : { i : i } %>
      <% end %>
    

    并使用内容创建部分 _user.html.erb

    <h3><%= i.firstname %><%= i.lastname %></h3>
    Birthday: <%= i.dateofbirth %><br />
    Address: <%= i.address %><br />
    City: <%= i.city %><br />
    Province: <%= i.province %><br />
    Phone Number: <%= i.phonenumber %><br />
    Service Provider: <%= i.serviceprovider %><br />
    Gender: <%= i.gender %><br />
    Languages: <%= i.languages %><br />
    

    【讨论】:

    • 这里有 Rails 快捷方式;你可以只渲染用户。
    猜你喜欢
    • 2018-12-11
    • 1970-01-01
    • 2012-02-08
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多