我基本上有消息和message_part
模型。我安装了carrierwave
,生成的图片上传器,:picture
和message
都有message_part
个列,我可以将图片上传到message
。当我将图片上传到message_part
时,此图片会覆盖所有其他图片,我在message
和message_part
中拥有相同的图片。
我的Message.rb
class Message < ActiveRecord::Base
belongs_to :speaker
has_many :messageparts
has_many :message_categories
has_many :categories, through: :message_categories
mount_uploader :picture, PictureUploader
end
我的message_part.rb
class Messagepart < ActiveRecord::Base
belongs_to :message
belongs_to :speaker
mount_uploader :picture, PictureUploader
end
我的message_part控制器创建
def create
# @message = Message.find(params[:message_id])
@messagepart = @message.messageparts.create(messagepart_params)
@messagepart.speaker_id = current_speaker.id
if @messagepart.save
redirect_to message_path(@message)
else
render 'new'
end
end
我的消息控制器
def create
@message = current_speaker.messages.build(message_params)
if @message.save
redirect_to messages_path
else
render 'new'
end
end
我对消息部分的看法
<%= simple_form_for([@message, @message.messageparts.build], html: { multipart: true } ) do |f| %>
<%= f.input :contentparttitle, label: "Part Title" %>
<%= f.input :contentpart, label: "Content Part", :as => :text, input_html: { :rows => 10, :class => "span6" } %>
<%= f.file_field :picture %>
<%= f.button :submit, label: "Add Parts", :class => 'btn-custom' %>
<% end %>
我对消息的看法
<%= simple_form_for @message, html: { multipart: true } do |f| %>
<%= f.input :title, label: "Message Title" %>
<%= f.input :description, label: "Short Description" %>
<%= f.input :content, :as => :text, :input_html => { 'rows' => 10} %>
<strong>Category:</strong>
<%= f.collection_check_boxes :category_ids, Category.all, :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
<% end %>
<%= f.file_field :picture %>
<%= f.button :submit %>
<% end %>
我的图片上传者
class PictureUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fit => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
答案 0 :(得分:0)
我想为两个模型使用两个单独的上传器,即使它们几乎相同。它可以保持您的对象的美观和整齐的名称,而不必担心覆盖。
和你的表格
<%= simple_form_for([@message, @message.messageparts.build], html: { multipart: true } ) do |f|%>
将同时更新@message
和@message.messageparts