What is `params.require(:person).permit(:name, :age)` doing in Rails 4?
up vote
122
down vote
favorite
All the examples of strong parameters in Rails 4 docs use
params.require(:person).permit(:name, :age)
Could someone please deconstruct and explain what is occurring with require and permit here?
ruby-on-rails-4
add a comment |
up vote
122
down vote
favorite
All the examples of strong parameters in Rails 4 docs use
params.require(:person).permit(:name, :age)
Could someone please deconstruct and explain what is occurring with require and permit here?
ruby-on-rails-4
2
This example comes directly from the documentation, which explainspermitbut notrequire.
– Erik Trautman
Aug 26 '13 at 2:17
add a comment |
up vote
122
down vote
favorite
up vote
122
down vote
favorite
All the examples of strong parameters in Rails 4 docs use
params.require(:person).permit(:name, :age)
Could someone please deconstruct and explain what is occurring with require and permit here?
ruby-on-rails-4
All the examples of strong parameters in Rails 4 docs use
params.require(:person).permit(:name, :age)
Could someone please deconstruct and explain what is occurring with require and permit here?
ruby-on-rails-4
ruby-on-rails-4
edited Nov 11 at 8:16
Soviut
56.4k33142208
56.4k33142208
asked Aug 25 '13 at 1:00
Erik Trautman
2,95322027
2,95322027
2
This example comes directly from the documentation, which explainspermitbut notrequire.
– Erik Trautman
Aug 26 '13 at 2:17
add a comment |
2
This example comes directly from the documentation, which explainspermitbut notrequire.
– Erik Trautman
Aug 26 '13 at 2:17
2
2
This example comes directly from the documentation, which explains
permit but not require.– Erik Trautman
Aug 26 '13 at 2:17
This example comes directly from the documentation, which explains
permit but not require.– Erik Trautman
Aug 26 '13 at 2:17
add a comment |
2 Answers
2
active
oldest
votes
up vote
160
down vote
accepted
The params in a controller looks like a Hash, but it's actually an instance of ActionController::Parameters, which provides several methods such as require and permit.
The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.
The permit method returns a copy of the parameters object, returning only the permitted keys and values. When creating a new ActiveRecord model, only the permitted attributes are passed into the model.
It looks a lot like the whitelisting that was formerly included in ActiveRecord models, but it makes more sense for it to be in the controller.
31
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond withtrueto thepermitted?method. By default, an instance of theActionController::Parametersclass will returnfalseforpermitted?Respondingtruetopermitted?means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.
– sameers
Nov 13 '13 at 18:05
2
Does chainingpermitonrequirealso permit and include the required parameters in the returned object?
– Dennis
Apr 6 '16 at 14:49
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
add a comment |
up vote
0
down vote
To be more precise, when you create for eg. doing .new(...), there must be :person hash indicated by require and the person hash will only accept :name and :age indicated by permit.
Example:
.new(person: { name: "Bhojendra", age: 32 }) // okay
.new(person: { name: "Rauniyar" }) // okay
.new(person: { name: "Bhojendra", other: 'asdf' }) // not okay
.new(person: { full_name: "Bhojendra Rauniyar" }) // not okay
.new(detail: { name: "Bhojendra", age: 32 }) // not okay, must be person
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
160
down vote
accepted
The params in a controller looks like a Hash, but it's actually an instance of ActionController::Parameters, which provides several methods such as require and permit.
The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.
The permit method returns a copy of the parameters object, returning only the permitted keys and values. When creating a new ActiveRecord model, only the permitted attributes are passed into the model.
It looks a lot like the whitelisting that was formerly included in ActiveRecord models, but it makes more sense for it to be in the controller.
31
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond withtrueto thepermitted?method. By default, an instance of theActionController::Parametersclass will returnfalseforpermitted?Respondingtruetopermitted?means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.
– sameers
Nov 13 '13 at 18:05
2
Does chainingpermitonrequirealso permit and include the required parameters in the returned object?
– Dennis
Apr 6 '16 at 14:49
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
add a comment |
up vote
160
down vote
accepted
The params in a controller looks like a Hash, but it's actually an instance of ActionController::Parameters, which provides several methods such as require and permit.
The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.
The permit method returns a copy of the parameters object, returning only the permitted keys and values. When creating a new ActiveRecord model, only the permitted attributes are passed into the model.
It looks a lot like the whitelisting that was formerly included in ActiveRecord models, but it makes more sense for it to be in the controller.
31
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond withtrueto thepermitted?method. By default, an instance of theActionController::Parametersclass will returnfalseforpermitted?Respondingtruetopermitted?means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.
– sameers
Nov 13 '13 at 18:05
2
Does chainingpermitonrequirealso permit and include the required parameters in the returned object?
– Dennis
Apr 6 '16 at 14:49
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
add a comment |
up vote
160
down vote
accepted
up vote
160
down vote
accepted
The params in a controller looks like a Hash, but it's actually an instance of ActionController::Parameters, which provides several methods such as require and permit.
The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.
The permit method returns a copy of the parameters object, returning only the permitted keys and values. When creating a new ActiveRecord model, only the permitted attributes are passed into the model.
It looks a lot like the whitelisting that was formerly included in ActiveRecord models, but it makes more sense for it to be in the controller.
The params in a controller looks like a Hash, but it's actually an instance of ActionController::Parameters, which provides several methods such as require and permit.
The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.
The permit method returns a copy of the parameters object, returning only the permitted keys and values. When creating a new ActiveRecord model, only the permitted attributes are passed into the model.
It looks a lot like the whitelisting that was formerly included in ActiveRecord models, but it makes more sense for it to be in the controller.
edited Aug 25 '14 at 15:31
0112
1,44141941
1,44141941
answered Aug 25 '13 at 7:50
fivedigit
14.6k54052
14.6k54052
31
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond withtrueto thepermitted?method. By default, an instance of theActionController::Parametersclass will returnfalseforpermitted?Respondingtruetopermitted?means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.
– sameers
Nov 13 '13 at 18:05
2
Does chainingpermitonrequirealso permit and include the required parameters in the returned object?
– Dennis
Apr 6 '16 at 14:49
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
add a comment |
31
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond withtrueto thepermitted?method. By default, an instance of theActionController::Parametersclass will returnfalseforpermitted?Respondingtruetopermitted?means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.
– sameers
Nov 13 '13 at 18:05
2
Does chainingpermitonrequirealso permit and include the required parameters in the returned object?
– Dennis
Apr 6 '16 at 14:49
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
31
31
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond with
true to the permitted? method. By default, an instance of the ActionController::Parameters class will return false for permitted? Responding true to permitted? means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.– sameers
Nov 13 '13 at 18:05
The description of permit is a bit off: permit returns another hash that contains only the permitted key AND (this is critical) will respond with
true to the permitted? method. By default, an instance of the ActionController::Parameters class will return false for permitted? Responding true to permitted? means the parameter object can be used in mass assignment; else the app will throw a ForbiddenAttributes error.– sameers
Nov 13 '13 at 18:05
2
2
Does chaining
permit on require also permit and include the required parameters in the returned object?– Dennis
Apr 6 '16 at 14:49
Does chaining
permit on require also permit and include the required parameters in the returned object?– Dennis
Apr 6 '16 at 14:49
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
I find the naming unfortunate, as require does a lot more than making a permitted parameter required. Using params.permit(:person, :name, :age) does not work, and generates errors like "Unpermitted parameters: :utf8" for a typical form.
– Damien
Aug 30 at 13:03
add a comment |
up vote
0
down vote
To be more precise, when you create for eg. doing .new(...), there must be :person hash indicated by require and the person hash will only accept :name and :age indicated by permit.
Example:
.new(person: { name: "Bhojendra", age: 32 }) // okay
.new(person: { name: "Rauniyar" }) // okay
.new(person: { name: "Bhojendra", other: 'asdf' }) // not okay
.new(person: { full_name: "Bhojendra Rauniyar" }) // not okay
.new(detail: { name: "Bhojendra", age: 32 }) // not okay, must be person
add a comment |
up vote
0
down vote
To be more precise, when you create for eg. doing .new(...), there must be :person hash indicated by require and the person hash will only accept :name and :age indicated by permit.
Example:
.new(person: { name: "Bhojendra", age: 32 }) // okay
.new(person: { name: "Rauniyar" }) // okay
.new(person: { name: "Bhojendra", other: 'asdf' }) // not okay
.new(person: { full_name: "Bhojendra Rauniyar" }) // not okay
.new(detail: { name: "Bhojendra", age: 32 }) // not okay, must be person
add a comment |
up vote
0
down vote
up vote
0
down vote
To be more precise, when you create for eg. doing .new(...), there must be :person hash indicated by require and the person hash will only accept :name and :age indicated by permit.
Example:
.new(person: { name: "Bhojendra", age: 32 }) // okay
.new(person: { name: "Rauniyar" }) // okay
.new(person: { name: "Bhojendra", other: 'asdf' }) // not okay
.new(person: { full_name: "Bhojendra Rauniyar" }) // not okay
.new(detail: { name: "Bhojendra", age: 32 }) // not okay, must be person
To be more precise, when you create for eg. doing .new(...), there must be :person hash indicated by require and the person hash will only accept :name and :age indicated by permit.
Example:
.new(person: { name: "Bhojendra", age: 32 }) // okay
.new(person: { name: "Rauniyar" }) // okay
.new(person: { name: "Bhojendra", other: 'asdf' }) // not okay
.new(person: { full_name: "Bhojendra Rauniyar" }) // not okay
.new(detail: { name: "Bhojendra", age: 32 }) // not okay, must be person
edited Nov 6 at 16:11
answered Nov 6 at 16:06
Bhojendra Rauniyar
49.7k1977120
49.7k1977120
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18424671%2fwhat-is-params-requireperson-permitname-age-doing-in-rails-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
This example comes directly from the documentation, which explains
permitbut notrequire.– Erik Trautman
Aug 26 '13 at 2:17