models/service.rb
has_many :service_files, :dependent => :destroy accepts_nested_attributes_for :service_files, :reject_if => lambda { |a| a[:uploaded_data].blank? }, :allow_destroy => true
models/service_files.rb
belongs_to :service
/service/new.html.erb & /service/edit.html.erb
<% form_for([:pro, @service], :url => {:action => :create},:html => {:multipart => true}) do |f| %> <!-- 子モデルにアップロードデータがあるので、multipart を指定 --> <% f.fields_for :service_files do |builder| %> <%= render "service_file_fields", :f => builder %> <% end %> <p><%= link_to_add_fields "Add", f, :service_files %></p> <% end %>
_service_file_fields.html.erb
<div class="fields"> <%= f.file_field :uploaded_data %> <%= link_to_remove_fields "<img src='/images/icons/cross.png'/>", f %> </div>※ application.js, application_helper.rb のメソッドは Railscasts 参照
はまったこと
undefined method `reflect_on_association' for NilClass:Class・・・が
問題は<%= link_to_add_fields t('common.add_obj', :obj => t('file.singular')), f, :service_files %> に正しいクラスを渡してなかった。
<% form_for(@service, :url => {:action => :create},:html => {:multipart => true}) do |f| %>
を下に変えたら問題なし。(service は pro のネームスペース下)
< <% form_for([:pro, @service], :url => {:action => :create},:html => {:multipart => true}) do |f| %>
0 件のコメント:
コメントを投稿