I''m tring to write a little function to indicate if a user is able to pay an invoice, I have my invoice model with: def canPay? if session[:user].id == self.consumer_id return true; else return false; end end as a method, but it gives the error: "undefined local variable or method `session'' for #<Invoice: 0xb7670928>" Right before the method is called, I put a breakpoint, and session is not nil, it has the user object in it as I expected it to. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stuart Grimshaw wrote:> I''m tring to write a little function to indicate if a user is able to > pay an invoice, I have my invoice model with: > > def canPay? > if session[:user].id == self.consumer_id > return true; > else > return false; > end > end > > as a method, but it gives the error: > > "undefined local variable or method `session'' for #<Invoice: > 0xb7670928>" > > Right before the method is called, I put a breakpoint, and session is > not nil, it has the user object in it as I expected it to.The session object is local to the controller (and the view rendered by the controller). The simple workaround is to pass in session[:user] as a parameter to canPay? -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Wang wrote:> Stuart Grimshaw wrote: >> I''m tring to write a little function to indicate if a user is able to >> pay an invoice, I have my invoice model with: >> > The session object is local to the controller (and the view rendered by > the controller). The simple workaround is to pass in session[:user] as a > parameter to canPay?And the reason why the session is not available is that doing so would break one of the core principles of the Model View Controller pattern: the model should be completely unaware of the presentation layer. You never want the model to use a session or an http request or generate html. The model should contain your business data and the logic to manipulate it. Anyway, sorry to be pedantic, but that''s why the session ain''t available... just pass the data you need to the method (or maybe some earlier work that the controller does with the invoice model puts it into a state where it is ready to answer the can_pay? message). b --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 25, 9:52 am, Ben Munat <bmu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Anyway, sorry to be pedantic, but that''s why the session ain''t > available... just pass the data you need to the method (or maybe some > earlier work that the controller does with the invoice model puts it > into a state where it is ready to answer the can_pay? message).Thanks Michael & Ben, makes perfect sense. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- ActiveRecord saving madness?
- Would someone like to tell me why this code will not solve my problem? (it''s short)
- AR transactions and isolation levels
- Invoice numbering for customers
- R-How to unlist data frame multiple structured list column value and new column