Error occurred while parsing request parameters.
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@users = User.all
end
def show
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user, notice: 'User was successfully created.'
else
render :new
end
end
def edit
end
def update
if @user.update(user_params)
redirect_to @user, notice: 'User was successfully updated.'
else
render :edit
end
end
def destroy
@user.destroy
redirect_to users_url, notice: 'User was successfully destroyed.'
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :email)
end
end
Explanation:
1. Define a UsersController
class that inherits from ApplicationController
.
2. Use a before_action
to set the user before specific actions (show, edit, update, destroy).
3. Define an index
action to retrieve all users and assign them to the @users
instance variable.
4. Define a show
action to display details of a specific user.
5. Define a new
action to create a new user instance.
6. Define a create
action to handle the creation of a new user. Redirect to the user's show page if successful, otherwise, render the new user form.
7. Define an edit
action to edit the details of a specific user.
8. Define an update
action to handle the update of user details. Redirect to the user's show page if successful, otherwise, render the edit user form.
9. Define a destroy
action to delete a user and redirect to the index page.
10. Define private methods set_user
to find a user by ID and user_params
to permit specific parameters (name, email) for user creation and update.