简体   繁体   English

Ruby on Rails,显示来自多个模型实例的多个属性

[英]Ruby on Rails, displaying multiple attributes from multiple model instances

In my Ruby on Rails project I have a Country model class and a Proposal model class. 在我的Ruby on Rails项目中,我有一个Country模型类和一个Proposal模型类。 Each Country has many Proposals and each Proposal belongs to a Country. 每个国家/地区都有许多提案,每个提案都属于一个国家/地区。 Each Proposal has 30 questions (attributes). 每个提案有30个问题(属性)。 In my view page, I want to display each question for every proposal that a country has. 在我的查看页面中,我想显示一个国家提出的每个提案的每个问题。

So far, I have: 到目前为止,我有:

<div class="relative"> 
   <% @country.proposals.each do |proposal| %> 
     <h2>Food and Medicine:</h2><br>
         <div class="relative2">
           <p>List of prohibited drugs:</p>
           <p><% proposal.q1 %></p>
         </div>
     <h2>Liquids and Gels:</h2><br>
         <div class="relative2">
           <p>List of prohibited liquids/gels:</p>
           <p><% proposal.q2 %></p>
           ...
      ...
   <% end %>
</div>

The headings appear fine whenever the country has a proposal associated with it. 只要该国家有与其相关的提案,标题就可以很好地显示。 However, the proposal attributes do not appear where I have placed them (in between the headings). 但是,投标属性没有出现在我放置它们的位置(在标题之间)。 I can confirm the attributes are not empty. 我可以确认属性不为空。

Proposal table: 提案表:

create_table "proposals", force: :cascade do |t|
  t.string   "status",     default: "Pending"
  t.text     "q1"
  t.text     "q2"
  ...
  ...
  t.text     "q29"
  t.text     "q30"
  t.datetime "created_at",                     null: false
  t.datetime "updated_at",                     null: false
  t.integer  "country_id"
end

You forgot an equal sign. 你忘记了等号。 You should do: 你应该做:

<p><%= @proposals_q2 %></p>

Instead of: 代替:

<p><% @proposals_q2 %></p>

In addition you should remove the equal sign from your loop. 另外,您应该从循环中删除等号。

<% @country.proposals.each do |proposals| %>

Instead of: 代替:

<%= @country.proposals.each do |proposals| %>

In ERB, <%= expression %> is used to inject the evaluated expression into the DOM. 在ERB中, <%= expression %>用于将评估后的表达式注入DOM。 <% expression %> is used to evaluate the expression and not do anything with it. <% expression %>用于评估表达式,不对其执行任何操作。 You don't want the result of @country.proposals.each {...} to be evaluated on the first line and injected into the DOM, so you want it without the equal sign, while you do want each of the instance variables to be injected into the DOM, so you want them with the equal sign. 希望在第一行对@country.proposals.each {...}的结果求值并注入到DOM中,因此您希望它没有等号,而您确实希望每个实例变量被注入到DOM中,因此您希望它们具有等号。

By the way, I assume that @proposals_q1 , @proposals_q2 , etc. are initialized in the controller action. 顺便说一句,我假设@proposals_q1@proposals_q2等在控制器操作中已初始化。 If instead you have q1 , q2 , etc. as attributes on the Proposal model, then instead of using @proposals_q1 you should do something like: 相反,如果您在Proposal模型上将q1q2等作为属性,那么应该使用以下方法代替使用@proposals_q1

<div class="relative"> 
   <% @country.proposals.each do |proposal| %> 
     <h2>Food and Medicine:</h2><br>
         <div class="relative2">
           <p>List of prohibited drugs:</p>
           <p><%= proposal.q1 %></p>
         </div>
     <h2>Liquids and Gels:</h2><br>
         <div class="relative2">
           <p>List of prohibited liquids/gels:</p>
           <p><%= proposal.q2 %></p>
           ...
      ...
   <% end %>
</div>

You're using <%= and <% backwards. 您正在反向使用<%=<% <%= outputs stuff to the page. <%=输出到页面。 <% runs Ruby code but doesn't output anything. <%运行Ruby代码,但不输出任何内容。

As it stands, you are outputting the result of <%= @country.proposals.each , and you are not outputting the <% @proposals_q1 . 就目前而言,您正在输出<%= @country.proposals.each的结果,而没有输出<% @proposals_q1 That means that, inside the loop, the line <% @proposals_q1 %> has absolutely no effect, but after the entire loop is done, <%= @country.proposals.each... outputs proposals . 这意味着,在循环内部,行<% @proposals_q1 %>绝对无效,但是在完成整个循环之后, <%= @country.proposals.each...输出proposals

You need to use <%= @proposals_q1 %> to output the contents of that variable, inside the markup being written inside your loop, and you need to use <% @country.proposals.each... %> because there is no useful output generated by that use of <%= . 您需要使用<%= @proposals_q1 %>来输出该变量的内容,并在循环内部编写的标记内,并且您需要使用<% @country.proposals.each... %>因为没有使用<%=产生的有用输出。

change this ' 改变这个

<% @proposals_q1 %> <%@ proposals_q1%>

" to this: " “对此:”

<%= @proposals_q1 %> <%= @ proposals_q1%>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM