gpt4 book ai didi

ruby-on-rails - Rails redirect_to 不起作用?

转载 作者:行者123 更新时间:2023-12-04 06:01:38 28 4
gpt4 key购买 nike

我已经尝试查看其他答案,但我似乎无法弄清楚为什么我的重定向不起作用。

所以我在 Rails 3.1 中使用 Devise,我正在制作一个购物网站。除非已登录,否则不允许访问者将商品添加到购物车。这就是我遇到的问题:如果他们未登录,我想将他们重定向到“商品”索引页面。这是我所拥有的:

class ItemsController < ApplicationController
def add_to_cart
@item = Item.find(params[:id])
if current_user
@item.update_attributes(:cart_id => @current_cart.id)
redirect_to :back
else
redirect_to categories_path, notice: 'You must sign in to add an item to your cart.'
end
end
.
.
.
end

到目前为止,当我单击要添加到购物车的链接时,该方法将被执行(我可以在服务器日志中看到 Rails 加载和定义 @item),并且它到达“else”语句,但没有发生重定向。

我已经为索引、新等(所有 RESTful 操作)生成了脚手架。另外,我确定我正在使用 add_to_cart 方法,因为我已经尝试使用一些 puts 语句进行调试。这里发生了什么事?

编辑:

另外,另一个可能有用的奇怪的东西......服务器似乎尝试执行此方法两次,并尝试“获取”类别两次:

Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.3ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 26ms

Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.2ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 25ms

Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 35ms (Views: 28.5ms | ActiveRecord: 4.2ms)

Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 37ms (Views: 30.6ms | ActiveRecord: 4.2ms)

编辑 2(根据 Delba 的要求)
resources :items do
member do
get 'add_to_cart'
end
end

编辑 3:更改 else 语句以响应 javascript
  respond_to do |format|
format.js { redirect_to items_path, notice: 'You must sign in to add an item to your cart.' }
end

最佳答案

对于可能需要回答此问题的任何人,只需将 redirect_to 语句替换为以下内容:

  respond_to do |format|
format.js
end

然后,在你的items下的views中,做一个add_to_cart.js.erb页面,由javascript组成来做通知,或者做任何事情。这是我放入的内容:
alert("Need to be signed in")

编辑:另外,对于它执行两次的部分:这有点无关,但出于某种原因,Rails 默认包含重复的 Javascript。具体来说,看看 application.js:它说需要 jquery 和需要 jquery_ujs。禁用其中之一,您就可以自由回家了。

要禁用这些 javascript 之一:

转到 Assets /application.js

删除 require jquery, require tree 之前的注释(//)。

这样,Rails 不会假定默认值,而是仅包含 jquery 和您在 assets/javascripts 中拥有的任何其他 javascripts

关于ruby-on-rails - Rails redirect_to 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844483/

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