Update Boolean attributes from another controller

Posted by sidonstackoverflow on Programmers See other posts from Programmers or by sidonstackoverflow
Published on 2013-11-03T09:59:57Z Indexed on 2013/11/03 10:13 UTC
Read the original article Hit count: 200

Filed under:

I have Users controller and session controller . I want to update one user attribute from session controller . How can i do that ?? I am currently using rails 4.0 .

Users controller:

   class UsersController < ApplicationController
    def show
    if Spec.find_by_user_id params[:id]
      @user = User.find(params[:id])
      @spec = Spec.find_by_user_id params[:id] 
    else
      if params[:id] == session[:id]  
      redirect_to spec_edit_path(params[:id])
      else
        redirect_to(community_index_path, {:notice => "Sorry there was an error"})
      end 
    end end
  def index
  end
  def new
    @user = User.new
  end
  def create
    @user = User.new(user_params)
    if @user.save  
        flash[:success] = "Welcome buddy !"
      redirect_to @user
    else
        render 'new'
    end
  end



  private
  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

end

Sessions Controller :

class SessionsController < ApplicationController
    def new

    end
    def create 
        user = User.find_by(email: params[:session][:email])
        if user && user.authenticate(params[:session][:password])
                session[:user_id] = user.id
                User.update(user.status, 'true')
                redirect_to root_url, :notice => 'You successfully logged in '
        else
          flash.now[:error] = 'Invalid email/password combination' # Not quite right!
          render 'new'
        end
    end
    def destroy
        session[:user_id] = nil
        redirect_to root_url, :notice => 'You successfully logged out '
    end

end

In above code when User logged in i just want to update my boolean column status at users table from sessions controller , but i failed . I am thankful to whom would like to answer my question !

© Programmers or respective owner

Related posts about ruby-on-rails