tap/app/controllers/users_controller.rb

69 lines
1.3 KiB
Ruby
Raw Normal View History

2014-11-06 16:25:27 +01:00
class UsersController < ApplicationController
2014-11-06 22:53:11 +01:00
before_action :logged_in_user, only: [ :index, :edit, :update, :show]
before_action :correct_user, only: [:edit, :update]
def index
2014-11-09 22:53:39 +01:00
@users = User.paginate(page: params[:page])
2014-11-06 22:53:11 +01:00
end
2014-11-06 20:07:18 +01:00
2014-11-06 16:25:27 +01:00
def new
2014-11-06 18:30:53 +01:00
@user = User.new
2014-11-06 16:25:27 +01:00
end
def show
@user = User.find(params[:id])
2014-11-09 22:53:39 +01:00
@orders = @user.orders.paginate(page: params[:page])
2014-11-06 16:25:27 +01:00
end
2014-11-06 18:30:53 +01:00
def create
@user = User.new(user_params)
if @user.save
2014-11-06 18:56:00 +01:00
log_in @user
flash[:success] = "Welcome to the Sample App!"
2014-11-06 18:30:53 +01:00
redirect_to @user
else
render 'new'
end
end
2014-11-06 20:07:18 +01:00
def order
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
redirect_to @user
else
render 'edit'
end
end
2014-11-06 18:30:53 +01:00
private
def user_params
params.require(:user).permit(:name, :marks)
end
2014-11-06 20:07:18 +01:00
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
2014-11-06 22:53:11 +01:00
store_location
2014-11-06 20:07:18 +01:00
flash[:danger] = "Please log in."
redirect_to login_url
end
end
2014-11-06 22:53:11 +01:00
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
2014-11-06 16:25:27 +01:00
end