Rails:redirect_to:尽管显示正确的日志,但仍未重定向

时间:2015-05-26 11:02:22

标签: javascript ruby-on-rails ruby angularjs coffeescript

我的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 controller

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 ($1, $2, $3, $4, $5, $6, $7) 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

我错过了某处?的诀窍吗?

1 个答案:

答案 0 :(得分:1)

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

要更详细地查看此内容,在提交表单时,会触发一些代码。此代码向服务器发送HTTP帖子。服务器执行操作,然后重定向 请求。您的javascript遵循幕后重定向,并加载新页面。所有这一切都发生在用户隐藏的状态,只影响javascript启动的HTTP请求,而不是主页本身。

为了使主页面窗口发生变化,你必须使用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"}