1

假设我有 2 个模型,例如 News、Clients。使用回形针的默认选项,我需要为它们中的每一个创建额外的列,例如 (photo_file_name .....) 但我只想创建不同的模型,比如说 Asset

资产.rb

  belongs_to :client
  has_attached_file :photo, :styles => {:small => "300x300>"}

客户端.rb

  has_one :asset, :dependent => :destroy
  accepts_nested_attributes_for :asset

客户端控制器.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 列,怎么办?

4

1 回答 1

1

就像 Mikhail Nikalyukin 说的,你应该打电话给

<%= image_tag @client.photo.url(:small) %>

代替

<%= image_tag @client.url(:small) %>
于 2012-01-23T15:00:26.607 回答