Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array of hashes with string values #325

Closed
dmitry opened this issue Nov 15, 2015 · 3 comments
Closed

Array of hashes with string values #325

dmitry opened this issue Nov 15, 2015 · 3 comments
Assignees
Labels

Comments

@dmitry
Copy link

dmitry commented Nov 15, 2015

I have a form with translations, which I'm generating with following code:

= f.simple_fields_for 'translations[]', translation do |ft|
  = ft.input :locale, as: :hidden
  = ft.input :name

In the result I'm receiving following parameters:

{"item"=>{"translations"=>[{"locale"=>"en", "name"=>""}, {"locale"=>"es", "name"=>""}]}}

Is it possible to filter such attributes with active_interaction?

I know it's possible with:

hash :translations, strip: false

But it's not strict enough, so I have to use strong_parameters, which I would like to reject at all in project.

Looks like logically this code should work:

    array :translations do
      string :name
      string :locale
    end

But it's not, as it's returns: attribute names in array block.

Should I make a PR with support of hash block in array, or it's possible to solve that somehow in other way around?

@tfausak tfausak self-assigned this Nov 15, 2015
@tfausak
Copy link
Collaborator

tfausak commented Nov 15, 2015

Does this work?

array :translations do
  hash do
    string :locale
    string :name
  end
end

array inputs can only have one input nested underneath them. This is because every element of the array must be the same type. And the inputs nested inside arrays cannot have names because they would never be used.

@dmitry
Copy link
Author

dmitry commented Nov 15, 2015

@tfausak thanks for the quick reply 👍.

I've tried that in the beginning, but looks like forgot something in integration with accepts_nested_attributes_for. Now it perfectly works. Thanks again!

@dmitry dmitry closed this as completed Nov 15, 2015
@tfausak
Copy link
Collaborator

tfausak commented Nov 15, 2015

No problem. Happy to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants