Forms Rails 3个字段用于现有记录/新记录、文件上载的不同布局
我有一个内容模型,其中有一个或多个音频文件需要通过新建/编辑表单添加。 我所做的是创建具有这种关系的模型:Forms Rails 3个字段用于现有记录/新记录、文件上载的不同布局,forms,file-upload,ruby-on-rails-3,nested-forms,Forms,File Upload,Ruby On Rails 3,Nested Forms,我有一个内容模型,其中有一个或多个音频文件需要通过新建/编辑表单添加。 我所做的是创建具有这种关系的模型: class Audio < ActiveRecord::Base belongs_to :content has_attached_file :audiofile, end class Content < ActiveRecord::Base ... has_many :audios accepts_nested_attributes_for :audio
class Audio < ActiveRecord::Base
belongs_to :content
has_attached_file :audiofile,
end
class Content < ActiveRecord::Base
...
has_many :audios
accepts_nested_attributes_for :audios, :allow_destroy => true
end
我正在使用Rails 3.0.3和回形针插件进行上传。对不起,如果问题太离题了
谢谢。根据我的记忆,您将能够访问语句字段中的对象实例 试试这样:
<% f.fields_for :audios do |audiof| -%>
<% if audiof.object.new_record? %>
<%= f.label :audiofile, 'Audio file:' %>
<%= audiof.file_field :audiofile %>
<% else %>
<%= "Filename = #{audiof.object.audiofile.filename}" %>
<%= "url = #{audiof.object.audiofile.url}" %>
<% end %>
<% end -%>
如果audiof.object返回nil(在这种情况下,它不是一个好名字),请通过显示所有公共方法进行检查
object方法应该返回一个Audio类的实例。谢谢,这就是我要找的,但不知道如何访问新的\u记录?方法。
@content.audios.build
<% f.fields_for :audios do |audiof| -%>
<% if audiof.object.new_record? %>
<%= f.label :audiofile, 'Audio file:' %>
<%= audiof.file_field :audiofile %>
<% else %>
<%= "Filename = #{audiof.object.audiofile.filename}" %>
<%= "url = #{audiof.object.audiofile.url}" %>
<% end %>
<% end -%>