我有2个模型:新闻和上载文件
class News < ActiveRecord::Base
has_many :uploadedfiles, as: :parent
attr_accessible :title, :content, :author
end
class Uploadedfile < ActiveRecord::Base
belongs_to :parent, polymorphic: true
has_attached_file :url
attr_accessible :url_file_name, :url_content_type, :url_file_size, :url_updated_at
end
并形成:
<%= form_for(@news) do |f| %>
<div class="field">
<%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf| %>
<%= uf.label :url %><br>
<%= uf.file_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
当我提交表单时,我的表格uploadedfile
未被更改
答案 0 :(得分:4)
我认为你有嵌套属性:uploadedfiles
class News < ActiveRecord::Base
has_many :uploadedfiles, as: :parent
attr_accessible :title, :content, :author, :uploadedfiles_attributes
accept_nested_attributes_for :uploadedfiles
end
以形式: 改变:
<%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf| %>
为:
<%= f.fields_for :uploadedfiles, Uploadedfile.new do |uf| %>
答案 1 :(得分:0)
我认为你不需要多态关联。这是一种更易读的方法:
class News < ActiveRecord::Base
has_many :uploadedfiles
attr_accessible :title, :content, :author
accept_nested_attributes_for :uploadedfiles
end
class Uploadedfile < ActiveRecord::Base
belongs_to :news
has_attached_file :url
attr_accessible :url_file_name, :url_content_type, :url_file_size, :url_updated_at
end
*请注意我已添加 accept_nested_attributes_for
形式:
<%= form_for(@news) do |f| %>
<div class="field">
<%= f.fields_for :uploadedfiles do |uf| %>
<%= uf.label :url %><br>
<%= uf.file_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>