2014-11-05 56 views
1

我正在使用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 %> 

如何将我的检讨控制器的样子,因为图像是从图像模型,其余是从审查模式?

+0

你试过嵌套属性:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html – Albin 2014-11-05 22:55:43

+0

我假设你的评论类'has_one:image'或类似的东西? – Albin 2014-11-05 22:56:44

+0

Image和Review模型之间的关系是什么? – 2014-11-05 22:56:56

回答

2

我会放下它的方式是这样的 -

模式

class Business < ActiveRecord::Base 

    has_many :images, :as => :imageable 
    has_many :reviews 

end 

class Image < ActiveRecord::Base 

    belongs_to :imageable, :polymorphic => true 

end 

class Review < ActiveRecord::Base 

    belongs_to :business 
    has_many :images, :as => :imageable 

    accepts_nested_attributes_for :images 

end 

我在accepts_nested_attributes_for :images添加,因为你需要让你的评论控制器将图像保存,而其本身的节约。我不确定你是如何设置业务控制器的,但我建议以同样的方式进行操作,它更清晰,更有意义。

评论查看 - 窗体视图

<%= form_for @review do |f| %> 
    <%= f.text_area :content %> 
    <%= f.fields_for :images do |photo| %> 
     <%= photo.file_field :photo, :multiple => false %> 
    <% end %> 
    <%= f.submit 'Submit Review with Image'%> 
<% end %> 

我已经改变了形式稍微使accepts_nested_attributes_for :images更好的工作。

最后审查控制器

查看控制器

class ReviewsController < ApplicationController 


    def new 

    @review = Review.new 
    @review.images.build 

    end 

    def create 

    @business = Business.find(id_of_business_your_reviewing) 
    @review = @business.reviews.build(review_params) 

    if @review.save 

     puts "Saved" 
     redirect_to "path" 
    else 

     puts "Something failed" 
     render 'new' 

    end 

    end 

private 

    def review_params 

    params.require(:review).permit(:content, :images_attributes => [:id, :photo]) 

    end 

end 

这将允许您创建一个新的审查,并通过图像添加照片吧。无需转到图像控制器,因为Review可以访问它并可以接受其属性。

我会建议在创建/编辑商家时实施类似于商业模式的东西。我会在业务控制器中执行这些操作,并使用accepts_nested_attributes_for :images来允许其接受其属性。

希望这有助于回答你的问题。如果我没有正确解释某事,请告诉我。

1

通过您的设计,您将无法知道哪些图像属于哪个评论,而是所有图像都将在该评论的父业务下。

一个好的设计是对图像模型使用多态关联。

class Business < ActiveRecord::Base 
has_many :images, as: :parent 
has_many :reviews 
end 

class Image < ActiveRecord::Base 
belongs_to :parent, polymorphic: true 
has_attached_file :photo 
end 

class Review < ActiveRecord::Base 
has_many :images, as: :parent 
belongs_to :business 
end 

,并使用两种可能的解决方案,我的情况下,建议1 注意,2号是更好之一。

如果您不知道如何构建多态关联,请检查this link


案例1对于有关系就像商业模式::

的模型,你可以:

1 - 添加一个新的动作到business_controller添加图像像你的代码使用你已经写在图像问题中的形式,但是具有到行动的路线。 形式应该有头象

form_for @image, :url => whatever_path_to_the_action 

2 - 使用模型accept_nested_attributes,并使用你已经在business_controller新编辑措施,而更多的代码和这些图像将与业务一起保存,无论它是新的还是只是编辑它以添加新图像并在视图中使用fields_for

看到这个链接,如果你不熟悉accept_nested_attributes和也this构造你的表格。

  • 如果您按照建议的设计进行操作,也是一样。

对于在你的设计像审查模式没有关系::

您可以添加动作reviews_controller add_image模型。

def add_image 
    @image = @review.business.images.new(image_params) 
    if @image.save 
    flash[:success] = "Image uploaded!" 
    redirect_to root_url 
    else 
    render 'YOUR FORM' 
    end 
end 

<%= form_for @image, url: path_to_the_action_add_image do |f| %> 
    <%= f.file_field :photo %> 
    <%= f.text_area :content %> 
    <%= f.submit 'Submit Review'%> 
<% end %> 

这不是一个好的设计,如果你有兴趣知道哪些图像属于哪个检查如果你想显示更多的图像添加与检讨

1

显示。你们之间应该有一些关系。我添加了has_one关系。这是理想的适合你的条件。您也可以添加has_many。您的模型应该是这样的:

class Business < ActiveRecord::Base 
has_many :images 
has_many :reviews 
end 

class Image < ActiveRecord::Base 
belongs_to :business 
belongs_to :review 
has_attached_file :photo 
end 

class Review < ActiveRecord::Base 
has_one :image 
belongs_to :business 
end 

你的形式应该是这样的:

<%= form_for @review do |f| %> 
    <%= f.text_area :content %> 
    <%= fields_for :image do |i| %> 
    <%= i.file_field :photo %> 
    <% end %> 
    <%= f.submit 'Submit Review'%> 
<% end %> 

如果您正在使用的轨道4变化需要在你的控制器:

def create 
    @review = Review.create(review_params) #This will create photo as well 
    redirect_to xyz_path 
end 

private 

    def review_params 
    params.require(:review).permit(:content, images_attributes: [:id, :photo, :_destroy]) 
    end 

您需要也添加数据库更改。所以相应地做出改变。 (在您的照片模型中添加review_id

我用了更简单的方法。您可以使用多态关联以及图像和其他模型。

+0

谢谢尼廷。这似乎没有工作。我不明白这一行:'tasks_attributes:[:id,:photo,:_destroy]'我在什么时候保存图像? – Bruno 2014-11-10 22:54:56

+0

对不起,我拼错了。它应该是'images_attributes'我已经更新了我的答案。 – 2014-11-11 06:21:08

相关问题