small medium large xlarge

South-park-famo_pragsmall
07 May 2009, 16:03
Stuart John Ackland (9 posts)

Hi everyone, Problem is as follows, with the store index displayed, when i press on an add to cart button it doesnt then display the add_to_cart view page, but instead stays on the store index page. No errors appear.

Set up = windows vista business / ruby version = 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] / rails version = 2.3.2

Code for session_store.rb

# Be sure to restart your server when you modify this file.

# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random, 
# no regular words or you'll be exposed to dictionary attacks.
ActionController::Base.session = {
  :key         => '_depot_session',
  :secret      => '0c1860a7b3d9c808de61d7c40e95ddef6144e7bac1fad624f98c8d288a3f2da1bc02231a7ea3fb10bad54e95d70e8c3c0ff77e86d0d144d6eaafa80871da10f0'
}

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rake db:sessions:create")
ActionController::Base.session_store = :active_record_store

Code for application_controller.rb

# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
end

Code for store_controller.rb

class StoreController < ApplicationController
  def index
  	@products = Product.find_products_for_sale 
  end
	
	def add_to_cart
		product = Product.find(params[:id])
		@cart = find_cart
		@cart.add_product(product)
	end



private

	def find_cart 
		session[:cart] ||= Cart.new
	end
		
end

Code for models/cart.rb

class Cart
	attr_reader :items
	
	def initialize
		@items = []
	end
	
	def add_product(product)
		@items << product
	end
end

Code for store/index.html.erb

<h1>Your Pragmatic Catalogue</h1>
<% for product in @products -%>
	<div class="entry">
		<%= image_tag(product.image_url) %>
		<h3><%=h product.title %></h3>
		<%= product.description %>
		<div class="price-line">
		<span class="price"><%= number_to_currency(product.price) %></span>
		<%= button_to "Add to Cart", :action => 'add_to_cart', :id => product %>
		</div>
	</div>
<% end %>

And finally code for store/add_to_cart.html.erb

<h2>Your Pragmatic Cart</h2>
<ul>
	<% for item in @cart.items %>
		<li><%=h item.title %></li>
	<% end %>
</ul>

If it’s something stupid that i have done, then many apologies. Any light you can shed on this will be much appreciated.

Generic-user-small
07 May 2009, 15:43
Kedar Mhaswade (21 posts)

Stuart,

One suggestion: While pasting the code, paste it within <pre><code> [your-code…] </code></pre>, so that it is much easier to read.

  • Restart the server and see if it changes. AFAICT, there is no obvious error.

-Kedar

South-park-famo_pragsmall
07 May 2009, 16:05
Stuart John Ackland (9 posts)

Thanks Kedar,

Apologies for the bad formatting. Have restarted server a few times with no change.

South-park-famo_pragsmall
07 May 2009, 16:52
Stuart John Ackland (9 posts)

I don’t know if this sheds any light, but the following is what appears in my console window after i have clicked on the add_to_cart button.

Processing StoreController#index (for 127.0.0.1 at 2009-05-07 17:34:45) [POST]
Parameters: {"authenticity_token"=>"JAECQ(yKilMxzPnIF/uKitGWyJsVjJB6kuBGhdV9kzI="}
£[4;36;1mProduct Load (1.0ms)£[0m   £[0;1mSELECT * FROM "products" ORDER BY title£[0m
Rendering template within layouts/store
Rendering store/index
Completed in 96ms (View: 87, DB: 2) ¦ 200 OK [http://localhost/store]

Please note where i have placed a £ sign in the console it is actually a left arrow symbol.

Generic-user-small
07 May 2009, 19:27
Jesse Blaine (1 post)

Have you double checked that the file is actually saved, Sounds like the button isn’t seeing a action.

Generic-user-small
08 May 2009, 01:39
Kedar Mhaswade (21 posts)

Forum debugging is rather hard ;), can you try something like: * sudo gem install ruby-debug * script/server –debugger * modify your add_to_cart controller with a statement like debugger (on a line all by itself).

Then, when you click on add_to_cart, the break-point would be hit. Please see rails guide for more information.

Hope that helps.

-Kedar

Russonajax_pragsmall
16 May 2009, 03:06
Russ Schissler (1 post)

In Rails 2.3.2, I couldn’t get this to work regardless of what I read and where I read it. I thought I had executed the entire truth table of possibilities so I shutdown the machine (a rare event, done in frustration) and went to bed. Alas! Today it worked.

Your success may vary, but … Follow the book including footnotes (its written to 2.2 folks); - config/initializers/session_store.rb - make changes as suggested (with some simple interpretive flexibility for 2.2 to 2.3 differences); - controllers/application_controller.rb - the problem line: protect_from_forgery # See ActionController:: … becomes a well behaved statement using the implied defaults. I also specified them changing the line to protect_from_forgery :only => [: create, :update, :destroy] both versions worked fine after shutdown and reboot.

South-park-famo_pragsmall
16 May 2009, 17:02
Stuart John Ackland (9 posts)

Russ, Kedar, Jesse… Really appreciate the time you have all spent on this. Unfortunately i still can’t seem to get to the bottom of this.

Can i just check something with you all regarding the following code:-

Processing StoreController#index (for 127.0.0.1 at 2009-05-07 17:34:45) [POST]
Parameters: {"authenticity_token"=>"JAECQ(yKilMxzPnIF/uKitGWyJsVjJB6kuBGhdV9kzI="}
£[4;36;1mProduct Load (1.0ms)£[0m   £[0;1mSELECT * FROM "products" ORDER BY title£[0m
Rendering template within layouts/store
Rendering store/index
Completed in 96ms (View: 87, DB: 2) ¦ 200 OK [<a href="http://localhost/store">http://localhost/store</a>]

Where it says ‘Rendering store/index’ should it not be saying ‘Rendering store/add_to_cart’???

The more i look into it the more i realise that everything is happening as it is meant to with the exception of rendering the new view. When i look at my cookies i can see the session being stored, it’s just not following through. :(

You must be logged in to comment