Proposed functionality in strong parameters to transform an attribute into nested_attributes format

122 views
Skip to first unread message

Austin Story

unread,
Mar 24, 2015, 10:23:01 PM3/24/15
to rubyonra...@googlegroups.com
Recently i've been working on a javascript frontend/ rails backend project.  

In one view their are several models that are posting to a users controller.  The user is accepting nested attributes for these 3 other models.  

When the params get there, they arrive as things like educations[], trainings[], courses[].  In order to get them to be accepted we put them through a transform that took each one and appended _attributes to the key. So educations => educations_attributes. 

It would be nice if strong parameters had a method.  Something like permit_nested that would do this translation if it didn't exist.  

I am happy to do the work and pr but wanted to get feed back first.  

Jason King

unread,
Mar 25, 2015, 12:56:19 PM3/25/15
to rubyonra...@googlegroups.com
This is a user error, the f.fields_for :educations construct (details here) is what you're after for this. It will result in the right `educations_attributes` params arriving at your controller.

Austin Story

unread,
Mar 25, 2015, 1:19:58 PM3/25/15
to rubyonra...@googlegroups.com
Hey Jason, that is correct.  But when you are using a frontend framework you don't get access to that and forcing Ember/Rails/[mvc framework] to do it in a rails way is more difficult than just transforming params on server side.  



--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/i-hAGOpJEHI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Jason King

unread,
Mar 25, 2015, 1:26:37 PM3/25/15
to rubyonra...@googlegroups.com
Then I think you should use a before_action in your controller(s). It's really not the place of strong parameters to make adjustments to the parameter names.

Andrey Novikov

unread,
Mar 26, 2015, 2:54:42 PM3/26/15
to rubyonra...@googlegroups.com
Hello Austin. We're using the Rails Resource library for AngularJS that does such things for us and happy with it (also it can convert attribute names from ruby's snake_case to js camelCase). Look for similar library for your framework, may be it exists.

среда, 25 марта 2015 г., 20:19:58 UTC+3 пользователь Austin Story написал:
Reply all
Reply to author
Forward
0 new messages