简体   繁体   中英

Rails redirect_to not working?

I've tried looking at other answers for this but I can't seem to figure out why my redirect isn't working.

So I'm using Devise with Rails 3.1, and I'm making a shopping site. Visitors aren't allowed to add things to their cart unless they are signed in. This is what I'm having trouble with: if they aren't signed in, I want to redirect them to the Items index page. Here's what I have:

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

As of right now, when I click the link to add to cart, the method gets executed (I can see Rails loading and defining @item in the server log), and it reaches the 'else' statement, but no redirect happens.

I've already generated scaffolding for the index, new, etc. (all the RESTful actions). Also, I'm sure that I'm reaching the add_to_cart method because I've tried debugging with some puts statements. What's happening here?

Also, another weird thing which may be of use... The server seems to try to execute this method twice, and tries to 'get' categories twice:

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)

resources :items do
  member do
    get 'add_to_cart'
  end
end

  respond_to do |format|
    format.js { redirect_to items_path, notice: 'You must sign in to add an item to your cart.' }
  end

For anyone who may need answers to this question, simply replace redirect_to statements with the following:

  respond_to do |format|
    format.js
  end

Then, in your views under items, make a add_to_cart.js.erb page, consisting of javascript to make notices, or do whatever. Here's what I put in mine:

alert("Need to be signed in")

EDIT: Also, for the part where it executes twice: this is somewhat unrelated, but for some reason by default Rails includes duplicate Javascripts. Specifically, look at application.js: it says require jquery and require jquery_ujs. Disable one of these and you're home free.

To disable one of these javascripts:

Go to assets/application.js

Remove the comments (the // ) before require jquery, require tree .

This way, Rails doesn't assume the default and instead includes only jquery and whatever other javascripts you have in assets/javascripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM