Access to view assistant for model in rails

I have an Adult model with a name attribute.

If the user is logged in, I want the name Adult.name to return only the first name.

Is there a way to bind helpers to a model where you can specify Adult.helper.name?

Or at least there are helpers placed in the model?

+4
source share
2 answers

Just explicitly include the helper in your model

# app/helpers/adults_helper.rb module AdultsHelper def say_hello "hello, world!" end end # app/models/adult.rb class Adult < ActiveRecord::Base include AdultsHelper end 

Console Testing

 $ script/console >> a = Adult.new # => #<Adult id:...> >> a.say_hello # => "hello, world!" 
+11
source

in the Adult model you can add

 def name self.first_name end 

so when you find adult for example

 a = Adult.last puts a.name #will print a.first_name 

Ok for a better explanation .. paste the code!

+2
source

Source: https://habr.com/ru/post/893520/


All Articles