2015-04-27 22 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