回形针的多态风格

时间:2013-11-11 08:02:48

标签: ruby-on-rails paperclip

我有一个多态模型,其中我的附件的样式是用Paperclip的lambda定义的。问题在于:attachable_idattachable_type等多态属性都是nil。没有控制器,因为我正在使用ActiveAdmin。

我的项目模型:

class Project < ActiveRecord::Base
  has_many :attachments, as: :attachable, :dependent => :destroy
  accepts_nested_attributes_for :attachments, allow_destroy: true
  attr_accessible :floors, :intro, :name, :price, :square, :deadline, :is_visible, :attachments_attributes
  validates_presence_of :floors, :intro, :name, :price, :square, :deadline
  validates_numericality_of :floors, :price, :square
  validates_length_of :name, :deadline, maximum: 60

  def set_styles
    { thumb: '150x90#', big: '1366x768#' }
  end
end

我的附件模型:

class Attachment < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
  attr_accessible :about, :position, :is_main_image, :is_blueprint, :attachment, :attachment_file_name, :attachable_attributes
  accepts_nested_attributes_for :attachable

  has_attached_file :attachment, styles: lambda { |a| a.instance.attachable.set_styles }
  validates_attachment_presence :attachment
  validates_attachment_size :attachment, :less_than => 3.megabytes
end

我的观点:

<%= semantic_form_for [:admin, @project], html: { multipart: true } do |f| %>
  <%= f.inputs do %>
    <%= f.input :is_visible, as: :boolean %>
  <% end %>
  <%= f.inputs do %>
    <%= f.input :name %>
    <%= f.input :intro %>
  <% end %>
  <%= f.inputs do %>
    <%= f.input :square %>
    <%= f.input :floors %>
    <%= f.input :price %>
    <%= f.input :deadline %>
  <% end %>
  <%= f.inputs do %>
    <%= f.input :attachments, as: :file, input_html: { multiple: true, name: 'project[attachments_attributes][][attachment]' } %>
    <% @project.attachments.each do |a| %>
      <div class="image-container-<%= a.id %>">
        <%= link_to image_tag(a.attachment.url :thumb), edit_admin_attachment_path(a.id), class: (a.is_main_image ? 'main_image' : 'not_main_image') %>
        <%= link_to 'Delete image', delete_attachment_admin_attachment_path(a.id), class: 'button', remote: true, method: :delete %>
      </div>
    <% end %>
  <% end %>
  <%= f.actions %>
<% end %>

0 个答案:

没有答案