Rails redirect_to不工作?

时间:2012-01-13 00:45:14

标签: ruby-on-rails ruby-on-rails-3 routes

我已经尝试过寻找其他答案,但我似乎无法弄清楚为什么我的重定向无效。

所以我正在使用Devise with Rails 3.1,而我正在建立一个购物网站。除非他们已登录,否则访客不得向他们的购物车添加内容。这就是我遇到的问题:如果他们没有登录,我想将他们重定向到Items索引页面。这就是我所拥有的:

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'语句,但没有重定向发生的情况。

我已经为索引,new等创建了脚手架(所有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

1 个答案:

答案 0 :(得分:3)

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

  respond_to do |format|
    format.js
  end

然后,在项目下的视图中,创建一个add_to_cart.js.erb页面,其中包含用于发出通知的javascript,或者执行任何操作。这就是我在我的内容:

alert("Need to be signed in")

编辑:此外,对于它执行两次的部分:这有点不相关,但由于某种原因,默认情况下Rails包含重复的Javascripts。具体来说,看看application.js:它说需要jquery并需要jquery_ujs。禁用其中一个,你就可以回家了。

要禁用其中一个javascripts:

转到assets / application.js

删除需要jquery之前的注释(//),需要树。

这样,Rails不会采用默认值,而只包含jquery以及您在assets / javascripts中拥有的其他任何javascripts