-
Notifications
You must be signed in to change notification settings - Fork 22k
Allow current_page?
to match against specific HTTP method(s) with a method:
option
#55286
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
Conversation
Maybe I'm dense, and this change is probably fine, but I don't understand in which case you'd need pagination helpers on a POST. |
@byroot |
@byroot The That could be pagination, but my targeted need is when the navigation includes "New Post" or "Edit Post" links that render a form that could be submitted and have validation errors. In those cases, there can still be a requirement for an "active" state on the navigation when rendering validation errors. That's not possible without a change like this PR. |
Yeah, sorry I had a bit of a brain fart. I skimmed over the historical issues, seems like that behavior was almost immediately regretted and people wanted it reverted but nothing happened. I need to find some clear headed time to see if there is an elegant solution to this. |
Ok, backtracking a bit here. I haven't done frontend work in a very long time, and even then I don't remember using Here's what the doc says:
With examples such as: current_page?(action: 'process')
# => false
current_page?(action: 'checkout')
# => true
current_page?(controller: 'library', action: 'checkout')
# => false My read on this is that this helper kinda made sense back in the Which begs the question of whether this need of your wouldn't be better solved by a newer method (or just a newer argument pattern) that is more "path helpers" native. e.g. your example: active: current_page?(new_post_path) || current_page?(posts_path, method: :post) What initially rubbed me the wrong way here is that In the end that method is still very much I'll try to review the code more in detail though. |
17c2254
to
dbf147e
Compare
check_parameters ||= options.is_a?(Hash) && options.delete(:check_parameters) | ||
if options.is_a?(Hash) | ||
check_parameters ||= options.delete(:check_parameters) | ||
method ||= options.delete(:method) |
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.
I don't think you can do ||=
here. it works for check_parameters
because it defaults to false
, bit method
defaults to a truthy value.
method_matches = if method == :get | ||
request.get? || request.head? | ||
else | ||
request_method = request.method.downcase.to_sym |
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.
We can use Request#method_symbol
here.
dbf147e
to
274e40f
Compare
@@ -539,24 +539,46 @@ def mail_to(email_address, name = nil, html_options = {}, &block) | |||
# current_page?('http://www.example.com/shop/checkout?order=desc&page=1') | |||
# # => true | |||
# | |||
# Let's say we're in the <tt>http://www.example.com/products</tt> action with method POST in case of invalid product. | |||
# Different actions may share the same URL path but have a different HTTP method. Let's say we |
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.
I think the indentation is wrong here.
274e40f
to
63b056f
Compare
… `method:` option
63b056f
to
0e19842
Compare
if options.is_a?(Hash) | ||
check_parameters = options.delete(:check_parameters) { check_parameters } | ||
method = options.delete(:method) { method } | ||
else | ||
options ||= options_as_kwargs | ||
end |
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.
I tweaked the options parsing code a bit.
Motivation / Background
The
current_page?
helper has historically only matched onGET
andHEAD
requests. This creates challenges when rendering content around validation errors on POST/PUT/PATCH actions.This PR adds an optional
method:
option that allows passing an explicit http method (or methods) to thecurrent_page?
helper to match specific http verbs.This makes it easier to render simple navigations like this:
This is necessary because both the
PostsController#index
andPostsController#create
share the same URL path but are differentiated by their HTTP method.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]