Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual attributes in rails 4

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

I am getting issue, here

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

I can reach above method, but not able to reach setter below, when trying to update/create.

  def tags_list=(tags)
    @tags = tags
  end
like image 714
Ashwin Yaprala Avatar asked Apr 19 '13 16:04

Ashwin Yaprala


1 Answers

Using virtual attributes in Rails 4 pretty much the same as with attr_accessible. You just have to add your virtual attribute to the permitted params in your controller (instead of attr_accessible), 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
like image 152
Chris Chattin Avatar answered Oct 01 '22 14:10

Chris Chattin