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

New cop that detects redirect_to '/' #856

Open
anthony-robin opened this issue Nov 3, 2022 · 7 comments
Open

New cop that detects redirect_to '/' #856

anthony-robin opened this issue Nov 3, 2022 · 7 comments

Comments

@anthony-robin
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I often see in projects code such as redirect_to '/', alert: 'Lorem ipsum' which is not the most elegant writing.
IMO the Rails route helper root_path seems a better way to handle it instead of hardcoding /.

Describe the solution you'd like

I would like a cop that throw an offense on this above line and suggest a replacement to redirect_to root_path, alert: 'Lorem ispum':

# Flag an offense
def index
  redirect_to '/', alert: 'Lorem ipsum'
end

# Autocorrect it
def index
  redirect_to root_path, alert: 'Lorem ipsum'
end

What do you think about this idea ?
Thank you :)

@andyw8
Copy link
Contributor

andyw8 commented Nov 3, 2022

Is there perhaps a more general rule here of not allowing redirect_to with a string literal?

@vlad-pisanov
Copy link
Contributor

@andyw8 sometimes it's necessary to redirect_to 'https://some-other-website.com'

@andyw8
Copy link
Contributor

andyw8 commented Nov 4, 2022

True, so perhaps it should only allow a string if it's a full URL (i.e. beginning http).

@andyw8
Copy link
Contributor

andyw8 commented Nov 4, 2022

This is actually kind of related to #856 but enforcing the opposite.

@koic
Copy link
Member

koic commented Dec 5, 2022

@andyw8 Is the link #856 wrong?

@andyw8
Copy link
Contributor

andyw8 commented Dec 5, 2022

Yes! But I can't find the one I meant, I think it was about named routes in tests.

@andyw8
Copy link
Contributor

andyw8 commented Dec 6, 2022

Ah, this I think: rubocop/rails-style-guide#328

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

No branches or pull requests

4 participants