2016-01-21 111 views
-1

我想將圖像添加到模型中。我有用戶設置Devise,他們have_many他們正在銷售的項目。Ruby on Rails - 圖片上傳

我現在想將一個圖像數組添加到Item模型(不知道這是否是最好的方法)。

我看了一個回形針,但只能做一個圖像。也看過載波,但不知道如何在現有模型上實現。

這是我的一些代碼。

item.rb的

class Item < ActiveRecord::Base 
    belongs_to :user 

    validates :title, presence: true 
    validates :price, presence: true 
    validates :description, presence: true 
end 

items_controller.rb

class ItemsController < ApplicationController 
    before_action :findItem, only: [:edit, :update, :sold] 

    def index 
    @items = Item.all 
    end 

    def new 
    @item = Item.new 
    end 

    def create 
    @item = Item.create(item_params) 
    @item.user = current_user 
    if @item.save 
     flash[:success] = "Your item was successfully listed." 
     render 'show' 
    else 
     flash[:error] = "Your item could not be listed. Please try again." 
     render 'new' 
    end 
    end 

    def show 
    @item = Item.find(params[:id]) 
    end 

    def edit 
    end 

    def update 
    if @item.update(item_params) 
     flash[:success] = "Your item listing was updated successfully." 
     redirect_to item_path(@item) 
    else 
     flash[:error] = "Your listing was not updated. Please try again." 
     render 'edit' 
    end 
    end 

    def sold 
    @item.toggle(:sold) 
    @item.save 
    redirect_to item_path(@item) 
    end 

    private 
    def item_params 
    params.require(:item).permit(:title, :price, :description) 
    end 

    def findItem 
    @item = Item.find(params[:id]) 
    end 
end 

表格項目/ new.html.erb

<div class="row"> 
    <div class="col-xs-12"> 
    <%= form_for(@item, :html => {class: "form-horizontal", role: "form"}) do |f| %> 
     <div class="form-group"> 
    <div class="control-label col-sm-2"> 
     <%= f.label :title %> 
    </div> 
    <div class="col-sm-8"> 
     <%= f.text_field :title, class: "form-control", placeholder: "What are you selling?", autofocus: true %> 
    </div> 
    </div> 

    <div class="form-group"> 
    <div class="control-label col-sm-2"> 
     <%= f.label :price %> 
    </div> 
    <div class="col-sm-8"> 
     <%= f.number_field :price, class: "form-control" %> 
    </div> 
    </div> 

    <div class="form-group"> 
    <div class="control-label col-sm-2"> 
     <%= f.label :description %> 
    </div> 
    <div class="col-sm-8"> 
     <%= f.text_area :description, rows: 10, class: "form-control", placeholder: "Describe your item. The more detail you include, the more likely it is to sell." %> 
    </div> 
    </div> 

    <div class="form-group"> 
    <div class="center col-sm-offset-1 col-sm-10"> 
     <%= f.submit class: "btn btn-primary btn-lg" %> 
    </div> 
    </div> 
<% end %> 

<div class="center col-xs-4 col-xs-offset-4"> 
    <%= link_to "[ Cancel and return to listing page ]", items_path %> 
</div> 

回答

0
#app/models/item.rb 
class Item < ActiveRecord::Base 
    has_many :images 
    accepts_nested_attributes_for :images 
    validates :title, :price, :description, presence: true 
end 

#app/models/image.rb 
class Image < ActiveRecord::Base 
    belongs_to :item 
    has_attached_file :picture 
end 

-

以上將允許您創建一個Item,這將有您需要的任何數量的images。您可以通過新的images通過accepts_nested_attributes_for

#app/controllers/items_controller.rb 
class ItemsController < ApplicationController 
    def new 
     @item = Item.new 
     @item.images.build 
    end 

    def create 
     @item = Item.new item_params 
     @item.save 
    end 

    private 

    def item_params 
     params.require(:item).permit(images_attributes: [:picture]) 
    end 
end 

#app/views/items/new.html.erb 
<%= form_for @item do |f| %> 
    <%= f.fields_for :images do |i| %> 
     <%= i.file_field :picture %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 
+0

那正是我所需要的。我會明天實施它,看看它是如何發展的。對於這些視圖,他們可以通過「@ item.images.each」訪問,然後遍歷? –

+0

如果你想*顯示*上傳圖片,你必須使用'@item.images.each do | image | <%= image_tag image.picture.url%> end' - 我可以看看如何將'picture.url'方法委託給您的圖片,但由於它是'has_many',所以它會非常棘手 –

2

你只是想創建一個圖像模型。並設置與Item的關係如下:Itemhas_many :imagesImagebelongs_to :item

但是,是的,回形針是一個好的開始。

編輯:哦,歡迎Rails,你會發現一個良好的社區準備好幫助。您也可能會發現搜索accepts_nested_attributes_for非常有用,因此您可以將圖像上載到您的項目窗體中,然後cocoon可以動態添加和刪除項目窗體上的圖像。

+1

感謝您的回答。那麼我是否正確地假設我將創建關聯與將「項目」與「用戶」關聯相同?我現在將前往Google並研究「accep_nested_attributes_for」和「cocoon」。 –

+0

是的,你是對的。一旦您知道我們已經回答您的原始問題,將此問題標記爲已解決。如果您有新問題,只需針對您遇到的具體問題創建新問題。 – Leito