2015-04-27 112 views
0

我有嵌套模型,船has_many :pictures,圖片belongs_to :boat。用戶創建了一艘船後,我使用了邪惡的寶石來製作步驟。最後一步是圖片步驟,用戶可以拖動圖片等等。它可以保存而不會出現問題。但是,當用戶放下圖片時,它會再次保存並呈現頁面(jQuery),以顯示添加的內容,因此用戶可以在此處銷燬圖片。嵌套模型邪惡,呈現頁面

但是,它呈現與空圖片的頁面,但是當我檢查它保存的數據庫。所以這可能是因爲boat_steps #show的行動。但我找不到。由於圖片頁面不屬於步驟完成嚮導路徑爲圖片。但我想改變它。我刷新頁面的原因是我無法完成圖片jquery。這是dropzonejs,但如果我在代碼中留下評論。我可以從數據庫中刪除照片,但它粘在屏幕上。

這裏是boat_steps controller

class BoatStepsController < ApplicationController 
    include Wicked::Wizard 


    before_action :logged_in_user 
    steps :model, :pricing, :description, :features, :picture 

    def show #PROBABLY I SHOULD CHANGE HERE 
     @boat = current_user.boats.last 
     @picture = @boat.pictures.new 
     render_wizard 
    end 

    def update 
     @boat = current_user.boats.last 
     @picture = @boat.pictures.new 
     case steps 
      when :picture 
       @picture.update(picture_params) 
       render_wizard @picture 
      else 
      @boat.update(boat_params) 
      render_wizard @boat 
     end 

    end 


private 

    def finish_wizard_path 
     new_boat_picture_path(@boat, @picture) 
    end 



    def boat_params 
     params.require(:boat).permit(:brand, :year, :model, :captained, :boat_type, :daily_price, :boat_length, :listing_tagline, :listing_description, :boat_category, :hull_material, :mast_material, :capacity, :sleeping_capacity, :private_staterooms, :fuel_type, :engine_horsepower, :fuel_capacity, :fuel_consumption, :draft) 
    end 

    def picture_params 
     params.require(:picture).permit(:name, :brand_id, :image) 
    end 


end 

這裏是picture.html.erb

<div class="container"> 

<%= form_for [@boat, @picture], html: { multipart: true, class: "dropzone", id: "picture-dropzone"} do |f| %> 


     <p> 

     <div class="fallback"> 
     <%= f.file_field :image %> 

     </div>  

     </p> 

<% end %> 

<p><%= link_to "Back to My Profile", current_user %></p> 


<% if @pictures.present? %> 
    <% @pictures.each do |pic| %> 
     </br> 
     <%= pic.name %> 
     <%= image_tag pic.image_url(:thumb).to_s %> 
     <%= link_to "edit", edit_boat_picture_path(@boat, pic) %> | 
     <%= link_to 'Destroy', boat_picture_path(@boat, pic), confirm: 'Are you sure?', method: :delete %> | 
     </br> 
    <% end %> 
<% end %> 



<script type="text/javascript"> 
$(document).ready(function(){ 
    // disable auto discover 
    Dropzone.autoDiscover = false; 

    // grap our upload form by its id 
    $("#picture-dropzone").dropzone({ 
     // restrict image size to a maximum 5MB 
     maxFilesize: 5, 
     // changed the passed param to one accepted by 
     // our rails app 
     paramName: "picture[image]", 

     acceptedFiles: "image/*", //CALISIYOR MU BILMIYORUm 
     // show remove links on each image upload 
     addRemoveLinks: false, 
     // if the upload was successful 
     success: function(file, response){ 
      // find the remove button link of the uploaded file and give it an id 
      // based of the fileID response from the server 
      //$(file.previewTemplate).find('.dz-remove').attr('id', response.fileID); 
      //$(file.previewTemplate).find('.dz-remove').attr('boat_id', response.boatID); 
      // add the dz-success class (the green tick sign) 
      $(file.previewElement).addClass("dz-success"); 
      location.reload(); //HERE IT RELOADS THE PAGE 
     }, 
     //when the remove button is clicked 
     //removedfile: function(file){ 

      // grap the id of the uploaded file we set earlier 
     // var id = $(file.previewTemplate).find('.dz-remove').attr('id'); 
     // var boat_id = $(file.previewTemplate).find('.dz-remove').attr('boat_id'); 
     // // make a DELETE ajax request to delete the file 
     // $.ajax({ 
     //  type: 'DELETE', 
     //  url: '/boats/' + boat_id + '/pictures/' + id, 
     //  success: function(file){ 
     //   removeFile(file); 

        //location.reload(); 

     //  } 
     // }); 
     //} 
    }); 
}); 



</script> 

EDIT 1:

這裏畫面控制器

class PicturesController < ApplicationController 
    before_action :logged_in_user 
    before_filter :load_parent 



    def new 
    @picture = @boat.pictures.new 
    @pictures = @boat.pictures.all 
    end 

    def show 
    @picture = @boat.pictures.find(params[:id]) 
    end 


    def create 

    @picture = @boat.pictures.new(picture_params) 

    if @picture.save 
    render json: { message: "success", fileID: @picture.id, boatID: @boat.id }, :status => 200 

    else 
     render json: { error: @picture.errors.full_messages.join(',')}, :status => 400 

    end 

    end 


    def edit 
    @picture = Picture.find(params[:id]) 
    end 

    def update 
    @picture = @boat.pictures.find(params[:id]) 

    if @picture.update_attributes(picture_params) 
     flash[:notice] = "Successfully updated picture." 
     render 'index' 
    else 
     render 'edit' 
    end 
    end 

    def destroy 

    @picture = @boat.pictures.find(params[:id]) 
    if @picture.destroy 
    #render json: { message: "File deleted from server" } 
     redirect_to new_boat_picture_path(@boat, @picture) 
     flash[:notice] = "Successfully destroyed picture." 
    else 
     render json: { message: @picture.errors.full_messages.join(',') } 
    end 
    #flash[:notice] = "Successfully destroyed picture." 
    #redirect_to new_boat_picture_path(@boat, @picture) 
    #redirect_to boat_pictures_path(@boat) 
    #redirect_to boat_path(@boat) 
    end 




    private 

    def picture_params 
     params.require(:picture).permit(:name, :image) 
    end 

    def load_parent 
    @boat = Boat.find(params[:boat_id]) 
    end 





end 

編輯2:

新演出的動作是;

def show 
     @boat = current_user.boats.last 
     @picture = @boat.pictures.new 
     @pictures = @boat.pictures.all 

     render_wizard 

    end 

但是當我降落在....boat_steps/picture URL,它顯示了一些空的照片,而不用行不通的。這是爲什麼?

這裏,

enter image description here

回答

0

在你的控制器,你只能分配@picture並在您的視圖您參考@pictures(未AFAIK設置)。

+0

如果我將它設置爲@picture,就像你說的那樣,它會爲每個對象指定一個錯誤'undefined method'爲'<圖片:0x007f835cb50158>' – Shalafister

+0

@picture指向您在窗體中使用的新空白圖片, @pictures = @ boat.pictures'。奇怪,你甚至不能讀你自己的代碼:) – nathanvda

+0

哈哈看了一下屏幕累了。但現在它顯示空的圖片沒有做任何事情。頁面來像img我上傳 – Shalafister