假设我有两个模型,如News、Clients。使用回形针的默认选项,我需要为每个回形针创建额外的列,如(photo_file_name…..)但我只想创建不同的模型,比如资产
资产.rb
belongs_to :client
has_attached_file :photo, :styles => {:small => "300x300>"}
客户端.rb
has_one :asset, :dependent => :destroy
accepts_nested_attributes_for :asset
clients_controller.rb
def new
@client = Client.new
@client.build_asset
end
_form.html.erb
<%= form_for @client, :html => {:multipart => true} do |f| %>
<%= f.fields_for :asset do |asset| %>
<%= asset.label :photo %><br/>
<%= asset.file_field :photo %>
<% end %>
<% end %>
目前,这是可行的,但如何在显示视图中显示它?我在做这个:
<%= image_tag @client.url(:small) %>
我知道这是不正确的,因为@client.asset没有url列,怎么做?
就像Mikhail Nikalyukin说的,你应该调用
<%= image_tag @client.photo.url(:small) %>
而不是
<%= image_tag @client.url(:small) %>