2014-11-05 55 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

相關問題