ROR accepted_nested_attributes_for与选择的一对多关系

时间:2012-04-14 05:18:25

标签: ruby-on-rails ruby-on-rails-3 nested-attributes

我有两个模型医生和问题,如下文:

Doctor Model

class Doctor < ActiveRecord::Base
has_many :questions
has_many :brands
accepts_nested_attributes_for :questions
end

问题模型

class Question < ActiveRecord::Base
belongs_to :discipline  
belongs_to :doctor
belongs_to :brand
end

现在您可以清楚地看到Doctor有很多问题和品牌,问题属于医生和品牌。我想从医生编辑页面向医生添加以前保存的问题。 我也想删除它们。我该怎么办?

我试过了:

<%= form.fields_for :questions, question,:child_index => (question.new_record? ? "index_to_replace_with_js" : nil) do |question_form| %>

  <table>
    <tr>
      <td>
        <div class="label">Select Question</div>
        <%= question_form.collection_select :id, Question.all, :id, :title ,{:include_blank => true } %>
      </td>          
    </tr>
  </table>
  

但这对我不起作用。你能给我一个适当的例子吗?

1 个答案:

答案 0 :(得分:1)

相关问题