Rails文件上传w / Paperclip和Polymorphic关联

时间:2016-05-01 02:41:42

标签: ruby-on-rails polymorphism paperclip

我正在使用具有位置,健身房,用户和图像的rails应用程序。所有这些共享图像,所以不是每个模型单独上传图像,我喜欢表达here的解决方案,并希望重新创建它。

现在我的模特看起来像这样

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  has_attached_file :image, style: { small: '64x64', medium: '100x100', large: '200x200' }
  validates_attachment :image, presence: true, content_type: { content_type: /\Aimage\/.*\Z/ }, 
                                                size: { in: 0..5.megabytes }
end

class Location < ActiveRecord::Base
  geocoded_by :county_name
  after_validation :geocode

  has_many :pictures, as: :imageable
  accepts_nested_attributes_for :pictures #added, not sure if needed or not
end

这里是位置控制器。我最关心的是控制器是我处理嵌套的参数

class Admin::LocationsController < ApplicationController
  before_action :authorize_admin!

  def new
    @location = Location.new
    @location.pictures.build
  end

  def create
    @location = Location.new(location_params)
    if @location.save
      flash[:success] = 'Location created!'
      redirect_to admin_location_path(@location)
    else
      render 'new'
    end
  end
  ...
  private
  def location_params
    params.require(:location).permit(:county_name, :description,
                                picture_attributes: [:location_id, :id, :image] )
  end
end

和我的观点

<section class="new-product">
  <div class="container">
    <div class="row white_panel">
      <div class="col-lg-12">
                <h2 class="text-center">Add New Location</h2>
                <hr>
                <%= form_for([:admin, Location.new], html: { multipart: true, class: 'form-horizontal' }) do |f| %>
                  <%= render 'shared/error_messages', object: f.object %>
                  <div class="form-group">
                      <%= f.label :county_name %><br>
                      <%= f.text_field :county_name, class: 'form-control' %><br>
                    </div>
                    <div class="form-group">  
                      <%= f.label :description %><br>
                      <%= f.text_area :description, class: 'form-control' %><br>
                    </div>
                  <p>Upload Picture</p>
                  <%= f.fields_for :picture, Picture.new do |image_upload| %> 
                    <%= image_upload.file_field :image, style: 'padding-bottom: 25px;' %>
                  <% end %>
                  <%= f.submit 'Go!', class: 'btn btn-gen np' %>
                <% end %> 
            </div>  
        </div>  
    </div>  
</section>

现在,我没有收到任何错误。位置很好,但图片表没有任何变化。它只是返回零。

有谁看到我做错了什么?我已经看到了一些如何做到这一点的资源,但它似乎通常是旧版本。

如果它可以帮助我使用任何人 rails 4.2.6
ruby 2.2.2p95
paperclip 4.3.6

任何关于如何处理这些关系的帮助都将非常感激:)

1 个答案:

答案 0 :(得分:0)

感谢this

,我能够解决这个问题

视图

echo $collection->getSelect()->__toString();

控制器

<%= form_for([:admin, Location.new], html: { multipart: true, class: 'form-horizontal' }) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
    <div class="form-group">
      <%= f.label :county_name %><br>
      <%= f.text_field :county_name, class: 'form-control' %><br>
    </div>
    <div class="form-group">  
      <%= f.label :description %><br>
      <%= f.text_area :description, class: 'form-control' %><br>
    </div>
    <p>Upload Pictures</p>
      <%= file_field_tag "images[]", type: :file, multiple: true, style: 'padding-bottom: 25px;' %>
    <%= f.submit 'Go!', class: 'btn btn-gen np' %>
 <% end %> 

现在我可以做到 class Admin::LocationsController < ApplicationController before_filter :authenticate_admin! def new @location = Location.new @location.pictures.build # I don't know if this is necessary or not end def create @location = Location.new(location_params) if @location.save # images if params[:images] params[:images].each do |image| @location.pictures.create(image: image, imageable_id: @location.id) end end flash[:success] = 'Location created!' redirect_to admin_location_path(@location) else render 'new' end end ... private def location_params # took out the other attr's params.require(:location).permit(:county_name, :description) end end Picture.find(1).image.url 一切正常:)