我有一个 Rails 应用程序,当用户嵌入视频时,我会在其中创建缩略图(所以我有一个 Image 模型和一个 Video 模型)。
我已经通过视频控制器成功创建了一个新图像。创建视频后,我想重定向到 Image 控制器的 image/create.js.erb 文件,传入新创建的视频缩略图。
images/create.js.erb 文件做了很多不同的事情来呈现更新的页面,所以我不想在我的 video/create.js.erb 文件中重复代码。
这就是我所拥有的:
视频控制器:
# 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">‹</a><a class="carousel-control right" href="#carousel-<%=@image.step_id%>" data-slide="next" style="display:none">›</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
}