Rails 文件上传与/ Paperclip 和多态关联
Rails file upload w/ Paperclip and Polymorphic assocations
我正在开发一个 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>
现在,我没有收到任何错误。位置做得很好,但图片没有任何变化table。它只是返回零。
有人看到我做错了什么吗?我已经看到了一些关于如何执行此操作的资源,但它似乎通常适用于旧版本。
如果它对我正在使用的任何人有帮助
rails 4.2.6
ruby 2.2.2p95
paperclip 4.3.6
任何有关如何处理这些关系的帮助都将不胜感激:)
多亏了this
,我才能够解决这个问题
查看
<%= 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
或 @locations.pictures.first.image.url
一切正常:)
我正在开发一个 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>
现在,我没有收到任何错误。位置做得很好,但图片没有任何变化table。它只是返回零。
有人看到我做错了什么吗?我已经看到了一些关于如何执行此操作的资源,但它似乎通常适用于旧版本。
如果它对我正在使用的任何人有帮助
rails 4.2.6
ruby 2.2.2p95
paperclip 4.3.6
任何有关如何处理这些关系的帮助都将不胜感激:)
多亏了this
,我才能够解决这个问题查看
<%= 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
或 @locations.pictures.first.image.url
一切正常:)