Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sample_app_ch9/app/controllers/users_controller.rb
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (55 sloc)
1.39 KB
This file contains 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
class UsersController < ApplicationController | |
before_action :signed_in_user, only: [:index, :edit, :update, :destroy] | |
before_action :correct_user, only: [:edit, :update] | |
before_action :admin_user, only: :destroy | |
def index | |
@users = User.paginate(page: params[:page]) | |
end | |
def show | |
@user = User.find(params[:id]) | |
@microposts = @user.microposts.paginate(page: params[:page]) | |
end | |
def new | |
@user = User.new | |
end | |
def create | |
@user = User.new(user_params) | |
if @user.save | |
sign_in @user | |
flash[:success] = "Welcome to the Sample App!" | |
redirect_to @user | |
else | |
render 'new' | |
end | |
end | |
def edit | |
@user = User.find(params[:id]) | |
end | |
def update | |
@user = User.find(params[:id]) | |
if @user.update_attributes(user_params) | |
flash[:success] = "Profile updated" | |
redirect_to @user | |
else | |
render 'edit' | |
end | |
end | |
private | |
def user_params | |
params.require(:user).permit(:name, :email, :password, | |
:password_confirmation) | |
end | |
# Before filters | |
def admin_user | |
redirect_to(root_url) unless current_user.admin? | |
end | |
def correct_user | |
@user = User.find(params[:id]) | |
redirect_to(root_url) unless current_user?(@user) | |
end | |
def destroy | |
User.find(params[:id]).destroy | |
flash[:success] = "User deleted." | |
redirect_to users_url | |
end | |
end | |