ruby on rails - What does 'require' do when defining strong parameter? -


i have simple app user model, table:

create_table "users", force: :cascade |t|   t.string "email"   t.string "name"   t.datetime "created_at", null: false   t.datetime "updated_at", null: false   .   .   . end 

in users_controller, define private method user_params pass permitted parameter, example:

 params.require(:user).permit(:email, :password, :password_confirmation, :name) 

my question require. in rubyonrails' api doc, says:

"when passed single key, if exists , associated value either present or singleton false ...... when given array of keys, method tries require each 1 of them in order. if succeeds, array respective return values returned ...... otherwise, method re-raises first exception found..."

but in previous example's app/views/users/new.html.erb page, if leave :name column blank, still can submit. tried change line this:

params[:user].permit(:email, :password, :password_confirmation, :name) 

it still works. although there's bit difference when passing parameter key :user.


rails can check column's presence using validation in model.rb. require here similar thing ?
what's difference between these 2 usages? explain bit, or give simple example?

thank you!

according documentation strong parameters:

allows choose attributes should whitelisted mass updating , prevent accidentally exposing shouldn't exposed

by defining

params.require(:user).permit(:email, :password, :password_confirmation, :name) 

you require params have user , permit user contain several fields. if either or third-party request doesn't contain user meaningful exception raised:

actioncontroller::parametermissing: param missing or value empty: user 

try not pass user when have following code in controller:

params[:user].permit(:email, :password, :password_confirmation, :name) 

you'll receive classical undefined method permit nil:nilclass that's way less readable

hope clarifies.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -