This repository was archived by the owner on Aug 17, 2017. It is now read-only.
Ensure that the key in require is valid.#148
Open
bryanrite wants to merge 1 commit intorails:masterfrom
Open
Conversation
Member
|
👍 for this, or a new method, which would force the hash type. right now it is really common to follow the pattern: def person_params
params.require(:person).permit(:name, :age)
endand if someone send a string on the person parameter, that would cause a 500 and not a 400. |
|
👍 |
|
I think I am running into a similar issue. it "should return 400 (Bad Request) when the job_template is missing an :id attribute" do
post(api_v1_jobs_path, {:job_template => "5ceb8bb8235bcc76bf475e21"}, @env)
response.status.should eql(400)
end
# => returns 500def create
params.require(:job_template).permit(:id)
render json: {}
endWould a better way to handle this be? # code not tested
def create
params.require(:job_template)
@job_template = JobTemplate.new(params[:job_template])
render :status => 400 and return unless @job_template.valid?
endI have not tried this yet since I have not yet created a JobTemplate model |
Contributor
Author
|
Ping? Can I help move this along in any way? Since valid JSON sent to a controller but not in the right "format" that strong params expects should return a 4xx... not a 500. My current solution to this (as I stated in #140) is: rescue_from NoMethodError do |exception|
if exception.message =~ /undefined method `permit' for/
render json: { message: "Helpful error message..." }, status: :bad_request
else
raise
end
endNot ideal but functional. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A proof of concept of ensuring that the key returned from require is valid. Can use from refactoring if this is a feature we want to accept.
This is to get around a NoMethod exception raised by valid JSON, but invalid input as explained in issue #140