redirect_to另一个控制器的create.js.erb

时间:2013-07-05 21:18:13

标签: ruby-on-rails

我有一个Rails应用程序,我在用户嵌入视频时创建缩略图(所以我有一个图像模型和一个视频模型)。

我已通过视频控制器成功创建了新图像。创建视频后,我想重定向到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文件。我怎么能这样做?

这是我的images / create.js.erb文件,因此您可以了解我不想在video / 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="<%=@image.image_path_url%>" rel="gallery <%=@image.step_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="<%=@image.image_path_url%>" rel="gallery <%=@image.step_id%>"><%=image_tag(@image.image_path_url(:preview), :width => "100%", :id=> @image.id)%></a></div>');
    $('.carousel').append('<a class="carousel-control left" href="#carousel-<%=@image.step_id%>" data-slide="prev" style="display:none">&lsaquo;</a><a class="carousel-control right" href="#carousel-<%=@image.step_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
}

1 个答案:

答案 0 :(得分:2)

我相信你可以做到:

render 'images/create'

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

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

希望有所帮助。