Ruby on Rails Dynamically generate field name in view using variable from view helper

In my view, let's say I have something like this:

<%= f.text_field :name_first %>

and another time this:

<%= f.text_field :name_last %>

Instead of hard-coding the field names, I want to dynamically generate them using a view helper method.

def category_name
    @contact_info.category.name
end

def return_field_name
  category_lower = category_name.downcase
  ":name"+category_lower  
end

So now my view should have something like this:

 <%= f.text_field return_field_name %>

Answers


Write:

def return_field_name
  category_lower = category_name.downcase
  "name#{category_lower}".to_sym
  # but I'm sure that without #to_sym it should work as well
end

But if it will help you you should write only pure methods. IMHO it's very good practice.


Need Your Help

Any way to restrict permissions at the subsite in sharepoint

sharepoint

I have a site collection and user A is having Design permissions in it.I created a subsite inheriting parent permissions and now I need to give just read permissions to user A in my subsite.