Skip to content

Instantly share code, notes, and snippets.

@almaron
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almaron/d7da253e32115851f33f to your computer and use it in GitHub Desktop.
Save almaron/d7da253e32115851f33f to your computer and use it in GitHub Desktop.
Jbuilder setting values in array loop
# Can I do something like this?
%w(text_field, another_text_field, some_more_text).each do |field|
json.field simple_format(@item.send(field.to_sym)
end
# Instead of this
json.text_field simple_format(@item.text_field)
json.another_text_field simple_format(@item.another_text_field)
json.some_more_text simple_format(@item.some_more_text)
@sevenseacat
Copy link

json.send field, simple_format(@item.send(field))

?

@almaron
Copy link
Author

almaron commented Jun 4, 2014

Ok. in case someone finds this gist.
The correct way to do this is as follows:

%w(whole bunch of text fields and even more).each do |field|
  json.set! field, simple_format(@item.send(field)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment