0
因此在respond_to块中,条件if stockUpdated and stock != {}
失败,并且else
块运行失败(我已使用byebug测试过)。所以服务器应该重定向,而是呈现。另一件奇怪的事情是,由于它的渲染flash.now[:alert] -'...'
应该工作,但也不会显示在视图中(请记住我的话)。为什么当我在respond_to块中使用redirect_to时,Rails 4渲染了我的视图
控制器:
respond_to do |format|
if stockUpdated and stock != {}
format.js
format.html {
redirect_to user_path(@user),
notice: "#{stock.symbol} has been added to your portfolio!"
}
else
flash.now[:alert] = "We could not add that stock to your portfolio."
format.html{redirect_to user_path(@user)}
end
end
服务器:
Started GET "https://stackoverflow.com/users/1" for ::1 at 2017-01-13 11:48:21 -0500
Processing by UsersController#show as HTML
Parameters: {"id"=>"1"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Stock Load (0.5ms) SELECT "stocks".* FROM "stocks" INNER JOIN "assets" ON "stocks"."id" = "assets"."stock_id" WHERE "assets"."user_id" = $1 [["user_id", 1]]
Rendered stocks/_stock.html.erb (0.5ms)
Rendered users/show.html.erb within layouts/application (13.7ms)
Completed 200 OK in 92ms (Views: 81.6ms | ActiveRecord: 2.0ms)
只是为了澄清:'user_path(@user)''是/用户/ 1'?如果是的话,那么你的服务器日志显示该路线(重定向的路线)。 – mrlew