Jestem świeżakiem w Railsie i zacząłem przygodę z nim czytając książkę Agile Programowanie w Rails wydanie II, oczywiście szybko się kapnołem, że jest nowsza wersja i wiele rzeczy jest zmienionych, tak więc zajrzałem do wersji angielskiej III. Niestety ta też jest nie aktualna:(
Nakreśle problem.
Zatrzymałem się na sesjach, gdyż pliki się inaczej nazywają no i trzeba było grzebać samemu.
Plik: 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
Plik: 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 => ‘38d1e05ebdc4e1690bc2504c6d3dd1230969f98734d3623af0ff010bbc2536e6015a8af5c2ea7e82cd41d93553e2b71d8a316dc7535061a098bbed3a1cef1d1e’
}
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
oczywiście zrobiłem rake db:sessions:create
w pliku store_controller.rb dopisałem:
private
def find_cart
session[:cart] ||= Cart.new
end
model/Cart.rb :
class Cart
attr_reader :items
def initialize
@items = []
end
def add_product(product)
@items << product
end
end
find_cart wywołuje poprzez inną funkcję przyciskiem button_to
Otrzymuje taki komunikat:
Processing StoreController#add_to_cart (for 127.0.0.1 at 2009-09-22 18:46:06) [POST]
Parameters: {“commit”=>“Dodaj do koszyka”, “authenticity_token”=>“0b3zL5QyRx+4BpTv2pemsbUpgXo09/Q/jdcySM+UWzI=”, “id”=>“10”}
no i odmawia posłuszeństwa.
Pracuje na Railsie 2.3.4 plus Mongrel
Nie wiem co już robić przeszukiwałem sieć badając czy ktoś ma ten sam problem, proszę o wskazanie mi gdzie jest błąd oraz jak to naprawić by działało.
Pozdrawiam
shadow