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
Allow f.select
to be called with a single hash containing options and HTML options
#46629
Conversation
I wrote something like this just today after going through the same steps of forgetting that it needed a second options hash to work, so I appreciate you looking to solve this! In my case, I was adding a class to a |
Hmm 🤔 if you maintained an allowlist of attributes for I think the allowlist would be |
I had a similar thought, and was actually surprised that the docs don't list the available options: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select I was also curious if a similar pattern could be applied to other form helpers with Maybe this is a case where the change makes sense for |
I wonder why distinction between |
Isn't there a backward compatibility issue here? What if an app has |
Those attributes don't get passed down to individual |
Nah, I'm the one who was confused. |
@casperisfine did you have any opinions on the change and/or on the allowlist ideal discussed above? |
Not really, but I don't do much frontend these days. But since no-one else reviewed it, I'll do it tomorrow. At first sight I don't see any issues with it, I'm just always a bit put of by "brittle" argument processing. But in this case I see how it's much more ergonomic so... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, please rebase and I'll merge.
…nd HTML options I do this a lot: ```erb <%= select :post, :author, authors, required: true %> ``` It doesn't work; the `required` attribute is ignored! Instead, you need to do this: ```erb <%= select :post, :author, authors, {}, required: true %> ``` It's hard to remember the right API, and it looks to me like a code smell. It looks even smellier when you end up with this: ```erb <%= select :post, :author, authors, { include_blank: "Choose an option" }, { required: true } %> ``` Where this would be nicer, but again, the `required` attribute is ignored: ```erb <%= select :post, :author, authors, include_blank: "Choose an option", required: true %> ``` This PR implements a special handling for `required`, `multiple`, and `size` HTML attributes so that these now do the same thing: ```erb <%= select :post, :author, authors, include_blank: "Choose an option", required: true %> <%= select :post, :author, authors, { include_blank: "Choose an option" }, { required: true } %> ``` ps. as proof I'm not the only person who makes this mistake, one of the tests in the Rails test suite was wrong! The test added in rails#40522 puts the `multiple` attribute in the wrong place and has the wrong assertion as as result. This PR includes a fix for the test.
408ede3
to
49c2e51
Compare
|
||
*Alex Ghiculescu* | ||
|
||
* Datetime form helpers (`time_field`, `date_field`, `datetime_field`, `week_field`, `month_field`) now accept an instance of Time/Date/DateTime as `:value` option. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @kryzhovnik I fixed a typo here (Datatime -> Datetime).
Implemented this in a small app. So much better already. Thanks @byroot ! |
@ghiculescu If one wants to set some class on the select element, does it still require to write |
Right now, yes. What did you think of this idea? |
I think that would be much better. |
Started working on this. Just a note on something I've observed so far which might explain why Both accept a
This means that it would not be possible to have an API where you never need to think about the additional hash. Here's some tests for the two options, both pass on def test_select_with_disabled_html_options_and_disabled_selected_option
@post = Post.new
@post.category = "foo"
assert_dom_equal(
"<select class=\"disabled\" disabled=\"disabled\" name=\"post[category]\" id=\"post_category\"><option value=\"\" label=\" \"></option>\n<option selected=\"selected\" value=\"foo\">foo</option>\n<option value=\"bar\">bar</option></select>",
select("post", "category", %w(foo bar), { prompt: true, include_blank: true }, { class: "disabled", disabled: true })
)
end
def test_select_with_disabled_option
@post = Post.new
@post.category = "foo"
assert_dom_equal(
"<select name=\"post[category]\" id=\"post_category\"><option value=\"\" label=\" \"></option>\n<option selected=\"selected\" value=\"foo\">foo</option>\n<option disabled=\"disabled\" value=\"bar\">bar</option></select>",
select("post", "category", %w(foo bar), { prompt: true, include_blank: true, disabled: ["bar"] })
)
end |
Urk, this makes me want to revert to be honest, as this is extremely confusing. What do you think @ghiculescu ? |
I still think this PR is fine but not sure if we should go further. My main evidence (apart from me liking it) is that some of the tests in Rails made the same mistake. Of course that’s all fairly subjective. I won’t be upset if you revert, but we should fix the broken test if you do (I can do that). |
Motivation / Background
I do this a lot:
It doesn't work; the
required
attribute is ignored! Instead, you need to do this:It's hard to remember the right API, and it looks to me like a code smell. It looks even smellier when you end up with this:
Where this would be nicer, but again, the
required
attribute is ignored:Detail
This PR implements a special handling for
required
,multiple
, andsize
HTML attributes so that these now do the same thing:Additional information
As proof I'm not the only person who makes this mistake, one of the tests in the Rails test suite was wrong! The test added in #40522 puts the
multiple
attribute in the wrong place and has the wrong assertion as as result. This PR includes a fix for the test.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]