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|
com开发者_JAVA百科ment.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.
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.
精彩评论