Rails: redirect_to:back 尽管显示了正确的日志但没有重定向

Rails: redirect_to:back not redirecting in spite of showing correct logs

我的html

<div class="modal-body center">         
      <form method="post">
        <textarea ng-model = "post.review" rows="10" cols="50" name="review" placeholder="something thougtful and helpful" style = "color: red;" class="fsField fsRequired fsTextAreaMaxLength", maxlength = "116"></textarea>

        <input ng-model = "post.rating" type="integer" name="rating" placeholder="0-10" style="color: red;" class="fsField fsRequired fsTextAreaMaxLength", maxlength = "116">

        <button ng-click = "submitReview(product)" class="btn btn-success" type="submit">Submit</button>

      <div ng-show = "duplicate_status" class = "submitStatus">*You have already submitted review for this Product</div>
      <div ng-show = "success_status" class = "submitStatus">*Your Review is successfully submitted</div>    
 </div>

我的javascript

$scope.submitReview = (product)->
  $scope.review =
    'review': $scope.post.review
    'rating': $scope.post.rating
    'product_id': product.id

  $http.post("/products/#{product.id}/review/new",$scope.review).success((data) ->
    if data == 'duplicate'
      $scope.duplicate_status = true

    if data == 'success'
      $scope.success_status = true    
)

rails控制器

def create
  if !current_user.present?
    redirect_to :controller => 'user_sessions', :action => 'create'
    return
  end
  if Review.where(:user_id => current_user.id, :product_id => params[:product_id]).first.present?      
   render plain: "duplicate"    
  end
  @review = Review.new(:review => params[:review],
                     :rating => params[:rating],
                     :product_id => params[:product_id],
                     :user_id => current_user.id,
                     :date => Time.now.to_date)   
  if @review.save        
    redirect_to :back             
  else
    redirect_to :back        
  end    
end

问题是在成功操作时,即在 @review.save 上,redirect_to :back 不会刷新页面。 这是尽管日志显示正确。

日志

Started POST "/products/2/review/new" for 192.168.1.88 at 2015-05-26 16:27:37 +0530
Processing by ReviewsController#create as JSON
  Parameters: {"review"=>"Test Review", "rating"=>"1", "product_id"=>"2"}
  User Load (0.6ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 3 LIMIT 1
  Review Load (0.6ms)  SELECT  "reviews".* FROM "reviews"  WHERE "reviews"."user_id" = 3 AND "reviews"."product_id" = 2  ORDER BY "reviews"."id" ASC LIMIT 1
   (0.3ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "reviews" ("created_at", "date", "product_id", "rating", "review", "updated_at", "user_id") VALUES (, , , , , , ) RETURNING "id"  [["created_at", "2015-05-26 10:57:37.245018"], ["date", "2015-05-26"], ["product_id", 2], ["rating", 1.0], ["review", "Test Review"], ["updated_at", "2015-05-26 10:57:37.245018"], ["user_id", 3]]
   (13.0ms)  COMMIT
Redirected to http://192.168.1.88:7001/product/2
Completed 302 Found in 23ms (ActiveRecord: 15.0ms)


Started GET "/product/2" for 192.168.1.88 at 2015-05-26 16:27:37 +0530


Started GET "/" for 192.168.1.88 at 2015-05-26 16:27:37 +0530
Processing by HomeController#index as JSON
  User Load (0.6ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 3 LIMIT 1
  Rendered shared/_invite_modal.html.erb (0.9ms)
  Rendered shared/_confirm_modal.html.erb (0.4ms)
  Rendered shared/_header.html.erb (10.8ms)
  Rendered shared/_footer.html.erb (0.4ms)
  Rendered home/index.html.erb (40.2ms)
Completed 200 OK in 43ms (Views: 42.3ms | ActiveRecord: 0.6ms)

问题是控制权被不必要地转移到 GET "/"

我是不是在某处错过了 return 的技巧?

您的问题是您只重定向了 javascript 发出的 XHR 请求——显示给用户的页面不受影响。

要更详细地了解这一点,当您提交表单时,您会触发一些代码。此代码向服务器发出 HTTP post。服务器做一些事情,然后重定向 that 请求。您的 javascript 在幕后遵循重定向,并加载新页面。所有这一切都对用户隐藏,只影响 javascript 触发的 HTTP 请求,而不影响主页本身。

为了使主页 window 发生变化,您必须使用 window.location.

明确告诉页面转到其他地方

例如:

$http.post("/products/#{product.id}/review/new",$scope.review).success((data) ->
    ...
    window.location = data.redirect_url

在控制器中:

render :json, {status: "duplicate", redirect_url: "/product/2"}