我是一個在軌道上進行紅寶石並開始我的第一個深入應用程序的新人。它有四個表格:問題,選項,答案和用戶。有一個問題列表,用戶可以投票選擇一個獨特的選項(存儲在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