سؤال

lets say I have 2 models like News, Clients. Using paperclip's default options, I need to create for each of them additional columns like (photo_file_name .....) but I just want to create different model, let's say Asset

asset.rb

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

client.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 %>

For now this is working, but how to show it in show view ? i'm doing this:

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

I know this is not correct, because @client.asset does not have url column, how to do it ?

هل كانت مفيدة؟

المحلول

Just like Mikhail Nikalyukin said, you should call

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

instead of

<%= image_tag @client.url(:small) %>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top