2016-09-01 6 views
2

ネストされたリソースの理解に問題があります。サービスの足場があり、症状モデルがあります。私は、サービスの足場にsymptom_item文字列を追加できるようにしたいと考えていました。表示テンプレートのネストされたリソース

これは私の現在のショーテンプレートコードです:ここで

<% if @service.symptoms.any? %> 
    <% @service.symptoms.each do |symptom| %> 
    <li><%= symptom.symptom_item %></li> 
    <% end %> 
<% else %> 
    <p> 
    none 
    </p> 
<% end %> 

は私のフォームコードです:

def service_params 
    params.require(:service).permit(:name, :s_text, :img_text, symptom_attributes: [:id, :service_item]) 
end 

私は:

<%= form_for(@service) do |f| %> 
    <% if @service.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@service.errors.count, "error") %> prohibited this service from being saved:</h2> 

     <ul> 
     <% @service.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :s_text %><br> 
    <%= f.text_area :s_text %> 
    </div> 
    <div class="field"> 
    <%= f.label :img_text %><br> 
    <%= f.text_field :img_text %> 
    </div> 

<%= f.fields_for :symptoms do |builder| %> 
    <div class="field"> 
    <%= builder.label :symptom_item %><br> 
    <%= builder.text_field :symptom_item, :rows => 3 %> 
    </div> 
<% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

ここsymptom_attributesを許可し、私のコントローラのコードがあります問題がショーテンプレートにあることを知ってください。私はのような単純なもの書くとき:

= @service.symptoms 

をそれが私にこのようなファンキーな何かを与える:

#<Symptom::ActiveRecord_Associations_CollectionProxy:0x000000095295c8> 

誰でもおそらくいくつかの他にどこショーテンプレートに私のコードが悪いのか、何を見ていますか?

class Service < ActiveRecord::Base 
    has_many :symptoms 
    accepts_nested_attributes_for :symptoms, allow_destroy: true 
end 

class Symptom < ActiveRecord::Base 
    belongs_to :service 
end 

答えて

0

は、それは私がsymptoms_attributesにsymptom_attributesを試してみました

def service_params 
    params.require(:service).permit(:name, :s_text, :img_text, symptoms_attributes: [:id, :symptom_item]) 
end 
+0

あなたの強いパラメータのsymptoms_attributesの代わりに、symptom_attributessymptom_itemの代わりservice_itemにする必要がありますが、何が:

ここに私のモデルは、念のためにあります。 – pk36

+0

強いパラメータで 'service_item'ではなく 'symptom_item'を試してください。編集された答え。 – Ren

関連する問題