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

Fix button_to's params option to support nested names. #17043

Merged
merged 1 commit into from
Feb 19, 2016

Conversation

jcoleman
Copy link
Contributor

In e6e0579 the params option was added to the button_to helper. However, the patch doesn't support nested hashes so {a: {b: 'c'}} for example gets turned into a hidden form input with the name 'a' and the value being the string representation of the {b: 'c'} nested hash.

Since Rails supports nested hashes everywhere else (and even in the URL params of link_to and button_to), I believe this to be a bug/unfinished feature.

@jcoleman
Copy link
Contributor Author

Note: the two tests I added may incorrectly report passing status even without all of my changes due to the fact that assert_dom_equals is broken (I've resolved the issue in rails/rails-dom-testing#18 )

@lustremedia
Copy link

+1 I am just running into this bug! This has not been merged I guess?

#
# {name: 'Denmark'}.to_query('country')
# # => [{name: 'country[name]', value: 'Denmark'}]
def to_form_params(namespace = nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is only used in one place. It probably doesn't need to live in Active Support for general use. IMO, it'd make more sense as a private method in ActionView::Helpers::UrlHelper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@georgeclaghorn I put it here because it parallels to_param/to_query, and because I think it's a potentially helpful helper in Rails projects. For example, I've used it to take a hash of values (that could have been serialized as JSON but instead needed to be used in a HTML form) and create a set of hidden input fields.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not add this to the public API.

@jcoleman
Copy link
Contributor Author

@lustremedia Sadly, no.

Anyone in the core team want to take a look at this?

@jljohnstone
Copy link

Thanks for doing this @jcoleman ! I'm hoping this gets added. It would be quite useful.

@jcoleman
Copy link
Contributor Author

@sgrif I just rebased this to master and ran the test suite again.

@jcoleman jcoleman force-pushed the fix-nested-params-in-button-to branch from bab4abd to 9390e18 Compare November 3, 2015 01:08
@jcoleman
Copy link
Contributor Author

jcoleman commented Nov 3, 2015

@sgrif I rebased to master and refactored the method I'd added in ActiveSupport into a function in UrlHelper to container the API leakage.

#
# to_form_params({name: 'Denmark'}, 'country')
# # => [{name: 'country[name]', value: 'Denmark'}]
def to_form_params(attribute, namespace = nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be marked as # :nodoc: to avoid adding it to the public API

@jcoleman
Copy link
Contributor Author

jcoleman commented Nov 4, 2015

@sgrif I've added :nodoc:

@maclover7
Copy link
Contributor

Please rebase.

@jcoleman
Copy link
Contributor Author

@maclover7 Rebased.

@bluehallu
Copy link

@jcoleman conflicts

@jcoleman jcoleman force-pushed the fix-nested-params-in-button-to branch 2 times, most recently from 5c12910 to 4019bc1 Compare February 13, 2016 18:58
@jcoleman
Copy link
Contributor Author

@bluehallu @maclover7 @sgrif This is becoming rather annoying. This will be at least the 3rd time I've rebased this pull request. Each time the reason is purely because it takes so long to hear back from a committer or maintainer after I rebase. Could we please get this merged in this time?

@jcoleman
Copy link
Contributor Author

Rebased. Again.

@kaldrenon
Copy link

+1 Please merge

@tminard
Copy link

tminard commented Feb 19, 2016

+1 Please merge this already

@lustremedia
Copy link

+1 Please merge!

#
# to_form_params({name: 'Denmark'}, 'country')
# # => [{name: 'country[name]', value: 'Denmark'}]
def to_form_params(attribute, namespace = nil) # :nodoc:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca Actually marked with the Ruby keyword private? It's already marked :nodoc:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This method should not be used by users (this is what # :nodoc: is for) and it is only used in this file (so it has private visibility)

@@ -71,6 +71,34 @@ def test_url_for_with_invalid_referer
assert_equal 'javascript:history.back()', url_for(:back)
end

def test_to_form_params_with_hash
assert_equal(
[{name: :name, value: 'David'}, {name: :nationality, value: 'Danish'}],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[{ name: :name, value: 'David' }, { name: :nationality, value: 'Danish' }],

In e6e0579 the `params` option was added to the `button_to` helper. However, the patch doesn't support nested hashes so `{a: {b: 'c'}}` for example gets turned into a hidden form input with the name 'a' and the value being the string representation of the `{b: 'c'}` nested hash.

Since Rails supports nested hashes everywhere else (and even in the URL params of link_to and button_to), I believe this to be a bug/unfinished feature.
@jcoleman jcoleman force-pushed the fix-nested-params-in-button-to branch from 4019bc1 to 459cd7f Compare February 19, 2016 16:09
@jcoleman
Copy link
Contributor Author

@rafaelfranca I've incorporated all of your suggested changes.

rafaelfranca added a commit that referenced this pull request Feb 19, 2016
Fix button_to's params option to support nested names.
@rafaelfranca rafaelfranca merged commit 4c5846f into rails:master Feb 19, 2016
@jcoleman
Copy link
Contributor Author

Thanks!

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Feb 20, 2016
rafaelfranca added a commit that referenced this pull request Feb 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants