ruby on rails - How do I populate an "attr_accessor" in my view? -


i’m using rails 5. have model following “attr_accessor” …

class scenario < applicationrecord   belongs_to :grading_rubric, optional: true   has_many :confidential_memo   has_many :scenario_roles, :dependent => :destroy   has_many :roles, :through => :scenario_roles    attr_accessor :roles_str    validates_presence_of :title   validates_presence_of :abstract    def roles_str     str = ""     roles.each |role|       str = str.empty? ? "role.name\n" : "#{str}#{role.name}"     end     str   end    end 

in view, display output of above “roles_str” method in “roles_str” text area. have set up

<%= form_for @scenario, :url => url_for(:controller => 'scenarios', :action => 'create') |f| %> …     <%= f.text_area(:roles_str, cols: 40, rows: 20, :validate => true) %> 

but when edit object, field not populated. how populate specified output model’s “role_str” method?

the empty text area because roles_str not attribute in database, rather simple accessor. populate text area, provide value manually, suggested @marsatomic in now-deleted answer.

<%= f.text_area(:roles_str, value: @scenario.roles_str, cols: 40, rows: 20, validate: true) %> 

the question is, going when form posted back? won't saved automatically (as there's no matching column in table). but, guess, it's out of scope of question. :)


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -