ruby - Rails on Heroku: session variables nil -
i've done lot of research , can't find similar questions/answers, appreciate help.
my app working great locally, when push heroku, chokes on session variables. specifically, throws nomethoderror nil:nilclass when try access attributes of object held in session. i've narrowed down session after switching params , having success. however, i'd prefer using session keeps code little cleaner.
i've poked around , seems happening every session object. quick context create new charge, new organization, , associate charge organization.
the protocol https (this checkout page) , these calls happening asynchronously via jquery, though i'm not sure if matters. rails 3.2. cedar/postgres on heroku. sqlite3/thin locally.
what's happening session on heroku?
production.rb
config.session_store :cookie_store, :key => '_my_app_session', :domain => :all
application_controller.rb
... helper_method :current_charge def current_charge @_current_charge ||= session[:current_charge_id] && charge.find_by_id(session[:current_charge_id]) end ...
charges_controller.rb
def create @charge = charge.new(params[:charge]) if @charge.save session[:current_charge_id] = @charge.id ... end
organizations_controller.rb
def create @organization = organization.new(params[:organization]) if @organization.save @charge = current_charge @charge.organization_id = @organization.id @charge.save! ... end
cedar log (thrown on organizations_controller.rb)
nomethoderror (undefined method `organization_id=' nil:nilclass):
when instantiate record using new
, record doesn't persist database until saved, , no id assigned record until persisted.
in code:
def create @charge = charge.new(params[:charge]) session[:current_charge_id] = @charge.id ... end
@charge.id
nil @ point, , therefore won't have id. you'll need use create
in charges_controller.rb give record id:
def create @charge = charge.create(params[:charge]) session[:current_charge_id] = @charge.id ... end
or use existing code , add save before setting session key:
def create @charge = charge.new(params[:charge]) @charge.save session[:current_charge_id] = @charge.id ... end
Comments
Post a Comment