2016-12-07 70 views
1

我對Rails開發非常陌生,在將多個圖像/附件保存到模型時遇到了問題。我的問題是,當我提交表單時,下面的代碼實際上並沒有保存到item_images表中。我遵循This Article作爲指導,儘管它似乎有點過時。我覺得在這一點上我有些茫然,所以我希望有人能指出我錯過了什麼。謝謝!Rails回形針寶石保存每個模型實例的多個附件

我有以下型號:

item.rb的

class Item < ActiveRecord::Base 
    has_many :item_images, :dependent => :destroy 

    accepts_nested_attributes_for :item_images, :reject_if => lambda { |t| t['item_image'].nil? } 
end 

item_image.rb

class ItemImage < ActiveRecord::Base 
    belongs_to :item 
    has_attached_file :image, 
         :styles => { thumb: "100x100#", small: "400x400#", large: "700x700" } 
    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ 
end 

我的控制器看起來是這樣的:

items_controller.rb

class ItemsController < ApplicationController 
    before_action :set_item, only: [:show, :edit, :update, :destroy] 

    # GET /items 
    # GET /items.json 
    def index 
    @items = Item.all 
    end 

    # GET /items/1 
    # GET /items/1.json 
    def show 
    end 

    # GET /items/new 
    def new 
    @item = Item.new 
    4.times {@item.item_images.build} 
    end 

    # GET /items/1/edit 
    def edit 
    4.times {@item.item_images.build} 
    end 

    # POST /items 
    # POST /items.json 
    def create 
    @item = Item.new(item_params) 

    respond_to do |format| 
     if @item.save 
     format.html { redirect_to @item, notice: 'Item was successfully created.' } 
     format.json { render :show, status: :created, location: @item } 
     else 
     format.html { render :new } 
     format.json { render json: @item.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /items/1 
    # PATCH/PUT /items/1.json 
    def update 
    respond_to do |format| 
     if @item.update(item_params) 
     format.html { redirect_to @item, notice: 'Item was successfully updated.' } 
     format.json { render :show, status: :ok, location: @item } 
     else 
     format.html { render :edit } 
     format.json { render json: @item.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /items/1 
    # DELETE /items/1.json 
    def destroy 
    @item.destroy 
    respond_to do |format| 
     format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_item 
     @item = Item.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def item_params 
     params.require(:item).permit(:title, :description, :price, :available, :sort_shop, :sort_gallery, :item_type, :size) 
    end 
end 

form.html.erb

<%= form_for @item, html: { multipart: true } do |f| %> 
    <% if @item.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2> 

     <ul> 
     <% @item.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %><br> 
    <%= f.text_area :description %> 
    </div> 
    <div class="field"> 
    <%= f.label :price %><br> 
    <%= f.text_field :price %> 
    </div> 
    <div class="field"> 
    <%= f.label :available %><br> 
    <%= f.check_box :available %> 
    </div> 
    <div class="field"> 
    <%= f.label :sort_shop %><br> 
    <%= f.number_field :sort_shop %> 
    </div> 
    <div class="field"> 
    <%= f.label :sort_gallery %><br> 
    <%= f.number_field :sort_gallery %> 
    </div> 
    <div class="field"> 
    <%= f.label :item_type %><br> 
    <%= f.text_field :item_type %> 
    </div> 
    <div class="field"> 
    <%= f.label :size %><br> 
    <%= f.text_field :size %> 
    </div> 

    <%= f.fields_for :item_images do |builder| %> 

    <% if builder.object.new_record? %> 

    <div class="field"> 
    <%= builder.label :image, "Image File" %> 
    <%= builder.file_field :image %> 
    </div> 

    <% end %> 

    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 

<% end %> 
+0

我想你需要在你的控制器中允許'item_images_attributes' –

回答

1

在項目控制器試試這個強參數

params.require(:item).permit(:title, :description, :price, :available, :sort_shop, :sort_gallery, :item_type, :size,item_images_attributes: [:image ])

比ItemImage.rb添加此line belongs_to :item, optional: true

,並移除item.rb的

此線

:reject_if =>拉姆達{| T |噸[ 'item_image']。爲零? } ` 如果你有任何錯誤,請回復

+0

這個工作!謝謝! – odinsride