2012-08-27 23 views
0

我有一個Report許多嵌套Value對象表:無法建立一個表格嵌套屬性

報告類

class Report < ActiveRecord::Base 
    attr_accessible :comments 
    has_many :values 
    accepts_nested_attributes_for :values 
end 

值類

class Value < ActiveRecord::Base 
    attr_accessible :value, :assessed_user_id, :behaviour_id 

    belongs_to :assessed_user 
    belongs_to :behaviour 

    belongs_to :report 

end 

我需要的形式,將接受Report及其相關矩陣Value這樣的對象:

 
| User | Behaviour1 | Behaviour2 | Behaviour3 | 
| Bob |  ___ | ___  | ___  | 
| Jane |  ___ | ___  | ___  | 
| Jill |  ___ | ___  | ___  | 

我新的軌道,我讀過所有關於嵌套的屬性和我試過在semantic_form_for一百萬的變化和可能性與formtastic,但我似乎無法將正確的value.value對象在他們的正確的地方或將其behaviourassessed_user設置爲隱藏字段,但我似乎無法使其工作。

下面顯示了應該隱藏的字段,我不能讓我的創建(接下來)接受它。另外,我沒有看到子窗體元素的所有索引:

<%= semantic_form_for @report do |f| %> 
    <table> 
    <tr> 
     <th>User</th> 
    <% @behaviours.each do |behaviour| %> 
     <th><%= behaviour.name %></th> 
    <% end %> 
    </tr> 
    <% index = 0 %> 
    <% @members.each do |member| %> 
     <tr> 
     <td><%= member.first_name %> <%= member.last_name %></td> 
     <% @behaviours.each do |behaviour| %> 
      <% @report.values[index].behaviour_id = behaviour.id %> 
      <% @report.values[index].assessed_user_id = member.id %> 
      <td> 

       <%= f.inputs :behaviour_id, :as => :hidden, :for => @report.values[index] %> 
       <%= f.inputs :assessed_user_id, :as => :hidden, :for => @report.values[index] %> 
       <%= f.inputs :value, :for => :values, :for => @report.values[index] %> 
       <% index = index + 1 %> 
      </td> 
     <% end %> 
     </tr> 
    <% end %> 

    </table> 
    <%= f.inputs :comments %> 

    <%= f.buttons %> 
<% end %> 

reports_controller.rb

def create 
    @report = Report.new(params[:report]) 
end 

回答

0
+0

觀看視頻給了我更好的理解,雖然我的表現並不完全相同,但它給我展示了許多我沒有完全理解的基礎知識。現在我正在使用基本的,更原始的版本。雖然這不是真正的答案,但確實讓我朝着一個好的方向發展。 –