Created
January 10, 2014 23:04
-
-
Save robskrob/8364397 to your computer and use it in GitHub Desktop.
controller_users
This file contains hidden or 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 :set_user, only: [:show, :edit, :update] | |
| before_action :require_same_user, only: [:edit, :update] | |
| def index | |
| @user = User.all | |
| end | |
| def show | |
| @user = User.find(params[:id]) | |
| end | |
| def new | |
| @user = User.new | |
| end | |
| def create | |
| @user = User.new(user_params) | |
| if @user.save | |
| flash[:notice] = "You are registered." | |
| redirect_to root_path | |
| else | |
| render :new | |
| end | |
| end | |
| def edit | |
| @user = User.find(params[:id]) | |
| end | |
| def update | |
| if @user.update(user_params) | |
| flash[:notice] = "profile updated" | |
| redirect_to root_path | |
| else | |
| render :edit | |
| end | |
| end | |
| def write_private_message | |
| @private_message = PrivateMessage.new | |
| @user = User.find(params[:id]) | |
| end | |
| def send_private_message | |
| @private_message = PrivateMessage.new(private_message_params) | |
| @notification = Notification.create(notificationable_type: @private_message, user: current_user, notification: params[:notification]) | |
| redirect_to user_path | |
| end | |
| private | |
| def private_message_params | |
| params.require(:private_message).permit(:sender_uid, :receiver_uid, :subject, :body) | |
| end | |
| def user_params | |
| params.require(:user).permit(:first_name, :last_name, :password) | |
| end | |
| def set_user | |
| @user = User.find(params[:id]) | |
| end | |
| def require_same_user | |
| if current_user != @user | |
| flash[:error] = "You're not allowed to do that" | |
| redirect_to root_path | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment