gpt4 book ai didi

javascript - rails : redirect_to:back not redirecting in spite of showing correct logs

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:06 26 4
gpt4 key购买 nike

我的 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 技巧?

最佳答案

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

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

为了改变主页面窗口,您必须使用 window.location 明确告诉页面转到其他地方。

例如:

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

在 Controller 中:

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

关于javascript - rails : redirect_to:back not redirecting in spite of showing correct logs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com