// app/views/users/edit.html.erb
<form action="/users/1" method="POST">
<input type="hidden" name="_method" value="PUT"
<input type="text" name="username" value="jtallant">
<input type="email" name="email" value="[email protected]">
<textarea name="bio">Bio goes here</textarea>
</form>
When this form is submitted, the UsersController#update action will be called
The params will look like the following:
params = {
username: 'jtallant',
email: '[email protected]',
bio: 'Bio goes here'
}
// app/controllers/user_controller.rb
class UsersController < ApplicationController
def update
@user = User.find(params[:id])
if @user.update(params) # Mass Assignment
flash[:notice] = "User successfully updated!"
redirect_to('/users')
else
render('edit')
end
end
end
@user.update(params)
# is the same as...
@user.update({
username: 'jtallant',
email: '[email protected]',
bio: 'Bio goes here'
})