So I’m mapping an object’s attributes to a table within a view and I wanted to make the code a little more succinct and eliminate redundant HTML.
Here’s what it looks like:
- @users.each do |user|
%tr
%td= user.username
%td= user.first_name
%td= user.last_name
%td= user.email
%td= user.country
%td= user.state
%td= user.password
...
I was wondering if there was a good way to eliminate all of those redundant %td= user. calls. I was watching the Railscast episode on Form Builders and he had a helper method that uses the following meta-programming syntax to eliminate repeats:
%w[text_field text_area password_field collection_select].each do |method_name|
define_method(method_name) do |name, *args|
Now is there any way I can use something similar to this structure? I actually tried to implement this exact syntax structure after replacing the method names and it seems that the define_method function was undefined (oh the irony!).
Use
.sendto invoke a method dynamically: