我正在使用Paperclip Gem。通过另一个视图上传图像
class Business < ActiveRecord::Base
has_many :images, :as => :imageable
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :photo
end
class Review < ActiveRecord::Base
belongs_to :business
has_many :images, :as => :imageable
end
这工作images/_form.html.erb
:
<%= form_for @image do |f| %>
<%= f.fields_for :photos_attributes do |photo| %>
<%= photo.file_field :image, :multiple => false %>
<% end %>
<%= f.submit 'Save' %>
<% end %>
图像控制器:
def create
@image = @business.images.new(image_params)
if @image.save
flash[:success] = "Image uploaded!"
redirect_to root_url
else
render 'new'
end
end
我希望能够上传图片也与评论。我想是这样的:
<%= form_for @review do |f| %>
<%= f.text_area :content %>
<%= f.fields_for :photos_attributes do |photo| %>
<%= photo.file_field :image, :multiple => false %>
<% end %>
<%= f.submit 'Submit Review with Image'%>
<% end %>
如何将我的检讨控制器的样子,因为图像是从图像模型,其余是从审查模式?
你试过嵌套属性:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html – Albin 2014-11-05 22:55:43
我假设你的评论类'has_one:image'或类似的东西? – Albin 2014-11-05 22:56:44
Image和Review模型之间的关系是什么? – 2014-11-05 22:56:56