Hi I have an hstore field in my database where I store a lot of different fields. I still want getters, setters and validations for my fields so I''ve created an array with the fields like this: DOCUMENT_FIELDS = %w[foo bar baz] Then I do some meta programming to create getters and setters: DOCUMENT_FIELDS.each do |field| define_method(field) do # .... end define_method("#{field}=") do |value| # ... end end Now I would like to pass all fields to strong parameters to properly filter it. I tried like this: params.require(:foo).permit(Foo::DOCUMENT_FIELDS.map(&:to_sym)) But this doesn''t work. It removes all values anyway. I guess it is because `Foo::DOCUMENT_FIELDS.map(&:to_sym)` creates an array that is passed to strong parameters (and it seems to not work with arrays). How can I get around this? Cheers, Linus -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1b71nouV0bAJ. For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Mar 8, 2013 at 3:27 AM, Linus Pettersson <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I have an hstore field in my database where I store a lot of different > fields. > > I still want getters, setters and validations for my fields so I''ve created > an array with the fields like this: > > DOCUMENT_FIELDS = %w[foo bar baz] > > Then I do some meta programming to create getters and setters: > > DOCUMENT_FIELDS.each do |field| > define_method(field) do > # .... > end > > define_method("#{field}=") do |value| > # ... > end > end > > Now I would like to pass all fields to strong parameters to properly filter > it. I tried like this: > params.require(:foo).permit(Foo::DOCUMENT_FIELDS.map(&:to_sym)) > > But this doesn''t work. It removes all values anyway. I guess it is because > `Foo::DOCUMENT_FIELDS.map(&:to_sym)` creates an array that is passed to > strong parameters (and it seems to not work with arrays). > > How can I get around this?`params.require(:foo).permit(*Foo::DOCUMENT_FIELDS.map(&:to_sym))` Though if I''m honest I probably would expect you to not take the long trip with map and to_sym, might as well just leave them as string keys because params has indifferent access with default keys being all string keys for security. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Mar 8, 2013 at 3:35 AM, Jordon Bedwell <envygeeks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> `params.require(:foo).permit(*Foo::DOCUMENT_FIELDS.map(&:to_sym))` > Though if I''m honest I probably would expect you to not take the long > trip with map and to_sym, might as well just leave them as string keys > because params has indifferent access with default keys being all > string keys for security.I forgot to say what *[] does. It will expand the array into args. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Got it myself! Splat operator to the rescue :) params.require(:foo).permit(*Foo::DOCUMENT_FIELDS.map(&:to_sym)) Den fredagen den 8:e mars 2013 kl. 10:27:50 UTC+1 skrev Linus Pettersson:> > Hi > > I have an hstore field in my database where I store a lot of different > fields. > > I still want getters, setters and validations for my fields so I''ve > created an array with the fields like this: > > DOCUMENT_FIELDS = %w[foo bar baz] > > Then I do some meta programming to create getters and setters: > > DOCUMENT_FIELDS.each do |field| > define_method(field) do > # .... > end > > define_method("#{field}=") do |value| > # ... > end > end > > Now I would like to pass all fields to strong parameters to properly > filter it. I tried like this: > params.require(:foo).permit(Foo::DOCUMENT_FIELDS.map(&:to_sym)) > > But this doesn''t work. It removes all values anyway. I guess it is because > `Foo::DOCUMENT_FIELDS.map(&:to_sym)` creates an array that is passed to > strong parameters (and it seems to not work with arrays). > > How can I get around this? > > Cheers, > Linus >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/7OPprSifa40J. For more options, visit https://groups.google.com/groups/opt_out.
Thank you. I thought strong params needed symbols. Cheers, Linus Den fredagen den 8:e mars 2013 kl. 10:35:13 UTC+1 skrev Jordon Bedwell:> > On Fri, Mar 8, 2013 at 3:27 AM, Linus Pettersson > <linus.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> wrote: > > Hi > > > > I have an hstore field in my database where I store a lot of different > > fields. > > > > I still want getters, setters and validations for my fields so I''ve > created > > an array with the fields like this: > > > > DOCUMENT_FIELDS = %w[foo bar baz] > > > > Then I do some meta programming to create getters and setters: > > > > DOCUMENT_FIELDS.each do |field| > > define_method(field) do > > # .... > > end > > > > define_method("#{field}=") do |value| > > # ... > > end > > end > > > > Now I would like to pass all fields to strong parameters to properly > filter > > it. I tried like this: > > params.require(:foo).permit(Foo::DOCUMENT_FIELDS.map(&:to_sym)) > > > > But this doesn''t work. It removes all values anyway. I guess it is > because > > `Foo::DOCUMENT_FIELDS.map(&:to_sym)` creates an array that is passed to > > strong parameters (and it seems to not work with arrays). > > > > How can I get around this? > > `params.require(:foo).permit(*Foo::DOCUMENT_FIELDS.map(&:to_sym))` > Though if I''m honest I probably would expect you to not take the long > trip with map and to_sym, might as well just leave them as string keys > because params has indifferent access with default keys being all > string keys for security. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/XjBdAERUM5cJ. For more options, visit https://groups.google.com/groups/opt_out.