我是一个在轨道上进行红宝石并开始我的第一个深入应用程序的新人。它有四个表格:问题,选项,答案和用户。有一个问题列表,用户可以投票选择一个独特的选项(存储在Answers连接表中),我试图让我的表头关联到表关联。Rails调查样式应用程序 - 在选项上显示所有答案
这是我已经设置我的个人RB文件:
class Question < ActiveRecord::Base
has_many :options
has_many :answers, :through => :options
end
class Option < ActiveRecord::Base
belongs_to :question
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
belongs_to :option
end
class User < ActiveRecord::Base
has_many :answers
has_many :questions, :through => :answers
end
我的问题控制器设置像这样包含的选项表:
@questions = Question.includes(:options).all
和我的index.html.erb文件中的表体:
<tbody>
<% @questions.each do |question| %>
<tr class="<%= cycle('lineOdd', 'lineEven') %>">
<td><%= question.question_text %></td>
<td><%= link_to 'Show', question %></td>
<td><%= link_to 'Edit', edit_question_path(question) %></td>
<td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% question.options.each do |option_text| %>
<tr class="backgroundColor1">
<td class="optionCell"> <%= option_text.option_text %> </td>
</tr>
<% end %>
<% end %>
</tbody>
在Question类中,我使用了'has_many:answers,:through =>:options' - 这是正确的方法,我将如何输出关联下的表格行中的总票数选项。
是否需要添加或更改问题控制器代码?
这是我的第一篇文章,对不起,如果我没有足够的信息!
感谢
真的令人耳目一新,看到第一个问题很好,但包括所有相关信息。做得好。 – max
啊谢谢 - 我现在非常享受Rails,所以我希望能够坚持下去! – classequalsarthur