1

I have a

class CommentsController < ApplicationController
  def foo
    session[:comments] ||= {}
    comment = Comment.new(params[:comment])
    # Validation and such
    session[:comments][comment.post_id] = comment
    #several redirections and remote authentications. User returns to another method, 
    # But for the sake of the example, we continue here. 
    CommentsController.publish_from_session
  end

  def self.publish_from_session
    session[:comments].each do |comment|
      comment.save!
    end
  end
end

This gives me a can't convert Symbol into Integer error. When diving into this, apparently session is simply not available, or not a hash. It might be that calling CommentsController.some_method is plain wrong.

What would be the correct solution?

Also: As mentioned in the commented code, the real deal is a bit more complex. The user returns either on another controller (sessions controller via oauth) or on yet another method on the CommentsController. See controllers calling eachother and Comments created after Oauth for how I came to this.

Community
  • 1
  • 1
berkes
  • 26,996
  • 27
  • 115
  • 206

1 Answers1

2

session is an instance method. You can't access it in a class method, but you can pass it (or just session[:comments] to the class method.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • I, indeed, managed to pass it. I was just hoping that it would be available somehow, by changing the way I call the method; that would keep the code a lot cleaner. – berkes Mar 08 '11 at 17:54
  • Well, you could either change `publish_from_session` to an instance method or create an instance method wrapper for it, depending upon your needs. – Craig Stuntz Mar 08 '11 at 18:02