ruby on rails - How to access virtual attribute from a controller -


i'm using rails 3.2.11 , ruby 1.9.3 in mac os x mountain lion

i have user model, , created virtual attribute called message

attr_accessor :message 

in code, try this:

if condition == true   self.message = "value true." else   self.message = "value not true" 

in controller, try this

def method   user = user.find(1)   flash[:message] = user.message end 

but flash message doesn't display anything. it's nil. how can pass value set in model controller? lot!

seems logic: when retrieve object db, there no reason it's attr_accessor should contain anything.

sticking logic, you'd rather:

  • remove attr_accessor

  • create method in user class.

like so:

def message    if condition     "value true."   else     "value not true"   end end 

be aware you're coupling data , view not practice.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -