2012-01-13 96 views
1

我試過尋找其他答案,但我似乎無法弄清楚爲什麼我的重定向不起作用。Rails redirect_to無法正常工作?

所以我使用Devise with Rails 3.1,並且我正在購物網站。訪客不允許將東西添加到他們的購物車中,除非他們已登錄。這是我遇到的問題:如果他們沒有登錄,我想將它們重定向到項目索引頁面。下面是我有:

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'語句,但不會發生重定向。

我已經爲index,new等生成了腳手架(所有的REST風格的動作)。另外,我確定我正在使用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)

EDIT 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 
+0

您是否使用了AJAX的形式? ('remote:true') – Damien 2012-01-13 00:54:18

+0

@Delba是的。這裏是我添加到購物車的鏈接:%= link_to'添加到購物車',{:controller =>「items」,:action =>「add_to_cart」,:id => item.id}, \t:remote => true %> – varatis 2012-01-13 00:58:57

+0

你不應該使用GET來添加'add_to_cart'。使用POST。請張貼您的路線。 – Damien 2012-01-13 00:59:46

回答

3

迴應任何人誰可能需要答案對於這個問題,只需將redirect_to語句替換爲以下內容:

respond_to do |format| 
    format.js 
    end 

然後,在您的項目下的視圖中,創建一個add_to_cart.js.erb頁面,其中包含javascript以進行通知,或執行任何操作。下面是我把我的:

alert("Need to be signed in") 

編輯:另外,對於它執行兩次部分:這是有點風馬牛不相及,但由於某種原因在默認情況下Rails包含重複的Javascript。具體來說,看看application.js:它說需要jquery並且需要jquery_ujs。禁用其中的一個,你可以免費回家。

要禁用這些Java腳本之一:

轉到資產/ application.js中

刪除註釋(//)之前需要的jQuery,需要樹。

這樣,Rails不承擔默認的,而是隻包括jQuery和你的資產/ JavaScript的任何其他的JavaScript