Virtual Attributes in Rails 4

How can I use virtual attributes (getter, setter) in rails 4, as removed by 'attr_accessible'.

I have a problem here

def tags_list @tags = self.tags.collect(&:name).join(', ') end 

I can use the method above, but I can not set the setter below when trying to update / create.

  def tags_list=(tags) @tags = tags end 
+8
ruby-on-rails ruby-on-rails-4 attr-accessor
source share
1 answer

Using virtual attributes in Rails 4 is pretty much the same as using attr_accessible. You just need to add your virtual attribute to the allowed parameters in your controller (instead of attr_accessible), and then add the getter and setter methods, as usual in your model.

 # your_controller.rb private def your_model_params params.require(:your_model_name).permit(:tags_list) end 
+12
source share

All Articles