2014-07-24 131 views
0

我有一個模型稱爲圖像與附加上載(載波)。圖片屬於名爲「清單」的模型。創建列表後,我被重定向到圖像索引頁面上傳文件(localhost:3000 /列表/ 1 /圖像空的子記錄創建後創建父記錄(Rails 4)

但由於某種原因,每次創建列表時,時間。實際上沒有圖像存在,但顯示每個圖像的「刪除」鏈接。

<span><%= link_to 'DELETE', listing_image_path(@listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span> 

任何幫助?謝謝。

房源控制器

class ListingsController < ApplicationController 
    before_action :set_listing, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user!, :except => [:show, :index] 

    def index 
     @listings = Listing.order('created_at DESC') 

     respond_to do |format| 
     format.html 
     format.json { render json: @listings } 
     end 
    end 

    def show 
    end 

    def new 
     @listing = Listing.new 
     @listing.user = current_user 
    end 

    def edit 
    end 

    def create 
     @listing = Listing.new(listing_params) 
     @listing.user = current_user 

     respond_to do |format| 
     if @listing.save 
      format.html { redirect_to listing_images_path(@listing), notice: 'Post was successfully created.' } 
     else 
      format.html { render action: 'new' } 
      format.json { render json: @listing.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def update 
     respond_to do |format| 
     if @listing.update(listing_params) 
      flash[:notice] = 'Deal was successfully updated.' 
      format.html { redirect_to @listing } 
      format.json { head :no_content } 
     else 
      format.html { render action: 'edit' } 
      format.json { render json: @listing.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def destroy 
     @listing.destroy 

     respond_to do |format| 
     format.html { redirect_to listings_url } 
     format.json { head :no_content } 
     end 
    end 

    private 
     # Use callbacks to share common setup or constraints between actions. 
     def set_listing 
     @listing = Listing.friendly.find(params[:id]) 
     end 

     # Never trust parameters from the scary internet, only allow the white list through. 
     def listing_params 
     params.require(:listing).permit(:id, :condition, :description, :nickname, :price, :size, :title, :user_id) 
     end 
end 

人數表

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

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

    <div> 
     <%= f.label :title %> 
     <%= f.text_field :title, :required => true %> 
    </div> 

    <div> 
     <%= f.label :price %> 
     <%= f.text_field :price %> 
    </div> 

    <div class="actions"> 
     <%= f.submit 'Submit', :class => 'buyNow' %> 
    </div> 
<% end %> 

圖像控制器

class ImagesController < ApplicationController 
    before_action :set_image, only: [:show, :edit, :update, :destroy] 
    before_filter :load_listing 

    def index 
     @images = @listing.images.load 
     @image = @listing.images.new 
    end 

    def new 
    end 

    def edit 
    end 

    def create 
     @image = @listing.images.new(image_params) 

     respond_to do |format| 
     if @image.save 
      format.html { redirect_to :back, notice: 'Image was successfully created.' } 
      format.json { head :no_content } 
     else 
      format.html { render action: 'new' } 
      format.json { render json: @image.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def update 
     respond_to do |format| 
     if @image.update(image_params) 
      format.html { redirect_to (@image.listing), notice: 'Image was successfully updated.' } 
      format.json { head :no_content } 
     else 
      format.html { render action: 'edit' } 
      format.json { render json: @image.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def destroy 
     @image = @listing.images.find(params[:id]) 
     @image.destroy 

     respond_to do |format| 
     format.html { redirect_to :back } 
     format.json { head :no_content } 
     end 
    end 

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

     def load_listing 
     @listing = Listing.find(params[:listing_id]) 
     end 

     # Never trust parameters from the scary internet, only allow the white list through. 
     def image_params 
     params.require(:image).permit(:file, :listing_id) 
     end 
end 

圖片在DEX

<div> 
    <%= form_for [@listing, @image], :html => { :class => 'uploadImages', :multipart => true } do |f| %> 
     <%= f.hidden_field :listing_id %> 

     <div> 
      <%= f.label :file, 'Upload Images' %> 
      <%= f.file_field :file, multiple: true, name: 'image[file]' %> 
     </div> 
    <% end %> 
</div> 

<div id="progress"></div> 

<% if @images.present? %> 
    <ul class="editGallery"> 
     <% @listing.images.each do |image| %> 
      <li> 
       <%= image_tag image.file_url(:list) if image.file? %> 
       <span><%= link_to 'DELETE', listing_image_path(@listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span> 
      </li> 
     <% end %> 
    </ul> 
<% end %> 
+0

當沒有圖像時單擊刪除時會發生什麼? – jkeuhlen

+0

我得到一個「沒有路由匹配[DELETE]」的錯誤,因爲沒有與該圖像關聯的ID。圖片網址通常是:../listings/1/images/ID#。對於那個空的圖像是:../listings/1/images。 @jkeuhlen – chrisbedoya

+0

你如何創建一個列表?從你在這裏展示的形式來看,你實際上傳了圖片。 – Ernest

回答

0

的問題是這一行:

@image = @listing.images.new 

這是建立一個新的圖像@listing,所以當你調用一個新的圖像包括圖像陣列中@listing.images.each。在爲它構建一個刪除鏈接之前,檢查圖像是否已經實際保存到數據庫中。

<% @listing.images.each do |image| %> 
    <% unless image.new_record? %> 
    <li> 
     <%= image_tag image.file_url(:list) if image.file? %> 
     <span><%= link_to 'DELETE', listing_image_path(@listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span> 
    </li> 
    <% end %> 
<% end %> 
+0

46秒後,你在計算這一個出來:( – jkeuhlen

+0

很好的捕獲,我沒有注意到這一個! – Ernest

+0

謝謝你你的幫助! – chrisbedoya

0

你的代碼,這部分看看:

<% if @images.present? %> 
<ul class="editGallery"> 
    <% @listing.images.each do |image| %> 
     <li> 
      <%= image_tag image.file_url(:list) if image.file? %> 
      <span><%= link_to 'DELETE', listing_image_path(@listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span> 
     </li> 
    <% end %> 
</ul> 

我相信你的問題是這樣的組合,你的圖像控制器的索引行爲。

當你點擊索引操作創建一個新的記錄@image = @listing.images.new

現在你@listing.images.each呼叫尚未保存的對象寄存器。