-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Rails/UnusedRenderContent suggests code that does not work in Rails 6.1 #1116
Comments
/cc @samrjenkins |
If my analysis is correct, perhaps the simplest thing is to restrict this lint to Rails >= 7? |
@shepmaster thanks for tagging me. I will get back to you shortly |
@shepmaster I apologise for the delay. It is the evening in the UK and I am starting to wind down 😴 I actually developed this cop mainly against Rails 6.1.7. I have also had a play around with the example code you provided. I think that the check this cop performs is still valid. However, the examples I provided in the documentation are not as helpful as they could be. When no content is provided to I think the guidance in the documentation should probably promote the use of In your particular example, I believe that the way to address the linting offence without seeing the ActionView::MissingTemplate error is to change your controller action from def index
render status: :no_content, json: {}
end to def index
head :no_content
end Please can you try this and let me know your thoughts? |
I can certainly try that and get back to you. Out of curiosity, does your app have templates for controllers you use |
The app which prompted me to write this cop does not have templates for endpoints which respond with However, we did have one legacy endpoint which was of the form: def update
render status: :no_content, json: { error_message: 'example error message' }
end which was obviously misleading because it gives the impression that the response will contain a JSON body when, in reality, Rails will drop the body content from the response. We changed this endpoint: def show
render status: :unprocessable_entity, json: { error_message: 'example error message' }
end |
Switching to
I'm not following when the current solution |
Right, it's the morning! I have a clearer brain 🤓 You are correct. The current suggested solution, for example: class UsersController < ActionController::Base
def index
render status: :no_content
end
end will only work when there is a matching template file such as It seems like, in reality, there is no scenario when you would want to use I think I feel the same way as you about having an unused template. This seems like an anti pattern which is misleading and should be avoided. I'll try to take a look at the documentation and examples today. Thanks for raising this 😊 |
@shepmaster I have revised the documentation on my fork. I will build a pull request later today. |
…recommend use of `head` instead of `render`
…ocumentation [Fix #1116] Update Rails/UnusedRenderContent documentation to recommend use of `head` instead of `render`
Personally, I prefer Actually, I'd really like to use If they added support for |
@jaredbeck i believe that |
Expected behavior
The lint should not suggest code that is invalid.
Actual behavior
The lint suggests code that is invalid in Rails 6.1.
Steps to reproduce the problem
Create a fresh Rails 6.1 application:
Create a basic controller and remove the views for the index:
Change the index to return
status: :no_content
with JSON:Request the index page:
Note the success:
Run RuboCop-Rails
Make the change, request the page, and note the error:
RuboCop version
RuboCop-Rails version
The text was updated successfully, but these errors were encountered: