2013-07-05 96 views
0

我有一个Rails应用程序,当用户嵌入一个视频(所以我有一个图像模型和一个视频模型)时,我创建一个缩略图图像。redirect_to另一个控制器的create.js.erb

我已经通过视频控制器成功创建了一个新图像。在创建视频时,我想重定向到Image控制器的image/create.js.erb文件,并为视频传入新创建的缩略图。

images/create.js.erb文件做了一堆不同的事情来呈现更新的页面,所以我不想重复我的video/create.js.erb文件中的代码。

这是我有:

video_controller

# POST /videos 
    def create 
    # need to validate 
    @video = Video.create(params[:video]) 
    # get the thumbnail image 
    thumbnail = @video.thumb_url 
    logger.debug "thumbnail video: #{thumbnail}" 
    @video.update_attributes(:thumbnail_url => thumbnail) 

    # create a new image record for the thumbnail 
    position = Step.find(@video.step_id).images.count # set the position of the added thumbnail to the last 
    @newImage = Image.new(:step_id=>@video.step_id, :image_path=> "", :project_id=>@video.project_id, :saved=> true, :position=>position) 
    @newImage.update_attributes(:remote_image_path_url => thumbnail) 
    @newImage.save 

    respond_to do |format| 
     if @video.save   
     format.js {redirect_to :controller=> "images", :action=>"create", :image=> @newImage} 
     else 
     format.json { render :json => @video.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

当我嵌入视频,它返回以下日志:

SQL (0.8ms) INSERT INTO "images" ("caption", "created_at", "image_path", "position", "project_id", "saved", "step_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["caption", nil], ["created_at", Fri, 05 Jul 2013 17:09:16 EDT -04:00], ["image_path", "default.jpg"], ["position", 0], ["project_id", 108], ["saved", true], ["step_id", 523], ["updated_at", Fri, 05 Jul 2013 17:09:16 EDT -04:00]] 
    (1.5ms) COMMIT 
    (0.1ms) BEGIN 
    (0.3ms) COMMIT 
    (0.3ms) BEGIN 
video id: O9k-MsfIkMY 
    (0.3ms) COMMIT 
Redirected to http://0.0.0.0:3000/images?image=1031 
Completed 302 Found in 2084ms (ActiveRecord: 7.4ms) 

Started GET "/images?image=1031" for 127.0.0.1 at 2013-07-05 17:09:17 -0400 
Processing by ImagesController#index as JS 
    Parameters: {"image"=>"1031"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1 
    Image Load (0.8ms) SELECT "images".* FROM "images" 
    Rendered images/index.html.erb within layouts/application (0.1ms) 
Completed 200 OK in 50ms (Views: 43.8ms | ActiveRecord: 1.2ms) 

所以没有正确重定向到image/create.js.erb文件。我怎样才能做到这一点?

这是我的图片/ create.js.erb文件,你可以在动作类型的想法,我不希望在我的视频/ create.js.erb文件重复:

<% if @image.new_record? %> 
    alert("Failed to upload image: <%= j @image.errors.full_messages.join(', ').html_safe %>"); 
<% else %> 

    $("#videoEmbedModal").modal('hide'); 

    var stepImagesCount = $('.image').length; 

    <% #add image to image gallery %> 
    $("#images").append("<%= j render(@image) %>"); 

    // load the slideshow if we're uploading the first image 
    if (stepImagesCount == 0){ 
    // move image form to the right gallery 
    $('.rightGallery').append($('#new_image')); 

    // add photo to carousel 
    $('.carousel-inner').append('<div class="item active"><a class="fancybox" href="<%[email protected]_path_url%>" rel="gallery <%[email protected]_id%>"> <%=image_tag(@image.image_path_url(:preview), :width => "100%", :id=>@image.id)%></a></div>'); 
    addImageButton("right"); 
    imageReset = true; 
    $('#blankPhoto').remove(); 
    $('.mainPhoto').show(); 
    } 
    else{ 
    // add photo to carousel 
    $('.carousel-inner').append('<div class="item"><a class="fancybox" href="<%[email protected]_path_url%>" rel="gallery <%[email protected]_id%>"><%=image_tag(@image.image_path_url(:preview), :width => "100%", :id=> @image.id)%></a></div>'); 
    $('.carousel').append('<a class="carousel-control left" href="#carousel-<%[email protected]_id%>" data-slide="prev" style="display:none">&lsaquo;</a><a class="carousel-control right" href="#carousel-<%[email protected]_id%>" data-slide="next" style="display:none">&rsaquo;</a>'); 
    } 

    // get step ID from carousel 
    var stepID = get_carousel_ID(); 
    setCarousel(stepID, stepImagesCount+1); 

    // remove upload 
    $('.upload').remove(); 

    // scroll to the bottom of the thumbGallery to show recently uploaded image 
    var thumbGalleryDiv = document.getElementById("images"); 
    thumbGalleryDiv.scrollTop = thumbGalleryDiv.scrollHeight; 

<% end %> 

function get_carousel_ID(){ 
    var carousel_id = $('.carousel').attr('id'); 
    var slashIndex = carousel_id.indexOf('-')+1; 
    var step_id = carousel_id.substring(slashIndex, id.length) 
    return step_id 
} 

回答

2

我相信你可以这样做:

render 'images/create' 

而不是它会达到同样的效果。

但是,如果您关心DRY代码,还有其他选择;你可以将不想重复的代码移动到另一个部分,只需从视图中调用该部分即可。这可能是一种更清洁,更模块化的解决方案,因为您在控制器中没有做任何超规范的事情。

希望有所帮助。

+0

这工作,谢谢!我只需将“@newImage”更改为“@image”即可。 – scientiffic

相关问题