簡體   English   中英

Rails redirect_to不工作?

[英]Rails redirect_to not working?

我已經嘗試過尋找其他答案,但我似乎無法弄清楚為什么我的重定向不起作用。

所以我正在使用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

對於可能需要此問題答案的任何人,只需將redirect_to語句替換為以下內容:

  respond_to do |format|
    format.js
  end

然后,在項目下的視圖中,創建一個由javascript組成的add_to_cart.js.erb頁面,以進行通知或執行任何操作。 這是我放入的內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM