Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:24:09+00:00 2026-05-31T18:24:09+00:00

I am trying to create an admin instance through my admins controller create action,

  • 0

I am trying to create an admin instance through my admins controller create action, but I keep getting an error that says:

ActiveRecord::RecordNotFound in AdminsController#show: Couldn't find User with id=4

The trace indicates that it is attempting to use the sessions helper (for user) instead of the appropriate adminsessions helper.

app/helpers/sessions_helper.rb:20:in `current_user'
app/helpers/sessions_helper.rb:12:in `signed_in?'
app/views/layouts/application.html.erb:13:in  
app_views_layouts_application_html_erb__1013605049_93953830

I can log in correctly and the admin is created. I just think the problem has to do with the redirect_to @admin in my admins controller, though I’m not sure.

How do I set it up so that my admins controller uses the adminsessions helper instead of the sessions helper? Any help would be greatly appreciated.

adminsessions_controller.rb

class AdminsessionsController < ApplicationController
  def new
    @title = "Log in"
  end

  def show
    @title = "Admin session"
  end  

  def create
    admin = Admin.authenticate(params[:adminsession][:email],
                               params[:adminsession][:password])
    if admin.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Log in"
      render 'new'
    else
      sign_in admin
      redirect_to admin
    end
  end

  def destroy
    sign_out
    redirect_to root_path                          
  end
end

admins_controller.rb

class AdminsController < ApplicationController

  def index
    @user = User.all
  end

  def show
    @admin = Admin.find(params[:id])
  end

  def new
    @admin = Admin.new
    @title = "New admin"
  end

  def create
     @admin = Admin.new(params[:admin])
    if @admin.save
      sign_in @admin
      flash[:success] = "Welcome admin!"
      redirect_to @admin
    else
      @title = "New admin"
      render 'new'
    end
  end
end

new.html.erb (form where I create new user)

<div id="signupform_new">
   <%= form_for(@admin) do |f| %>
   <div class="field">
     <%= f.label :username %>
     <%= f.text_field :name, :class => "round"  %>
   </div>
   <div class="field">
     <%= f.label :email %>
     <%= f.text_field :email, :class => "round" %>
    </div>
    <div class="field">
      <%= f.label :password %>
      <%= f.password_field :password, :class => "round"  %>
    </div>
    <div class="field">
       <%= f.label :password_confirmation, "Confirmation" %>
       <%= f.password_field :password_confirmation, :class => "round"  %>
    </div>
    <div class="action">
      <%= button_tag "", :class => "acctSubmit" %>
    </div>
  <% end %>
</div>

sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    session[:user_id] = user.id
    self.current_user = user
  end


  def signed_in?
   !current_user.nil?
  end

  def current_user=(user)
   @current_user = user
  end

  def current_user
   @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def current_user?(user)
   user == current_user
  end

 def authenticate
  deny_access unless signed_in?
 end    

 def sign_out
  session[:user_id] = nil
  self.current_user = nil
end

 def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  clear_return_to
end

def deny_access
  store_location
  redirect_to login_path, :notice => "Please log in to access this page."
end

private

  def store_location
    session[:return_to] = request.fullpath
  end

  def clear_return_to
    session[:return_to] = nil
  end
end

adminsessions_helper.rb

module AdminsessionsHelper


def sign_in(admin)
  adminsession[:admin_id] = admin.id
  self.current_admin = admin
end


def signed_in?
  !current_admin.nil?
end

def current_admin=(admin)
  @current_admin = admin
end

def current_admin
  @current_admin ||= Admin.find(adminsession[:admin_id]) if adminsession[:admin_id]
end

def current_admin?(admin)
  admin == current_admin
end

def authenticate
  deny_access unless signed_in?
end 

def sign_out
  adminsession[:admin_id] = nil
  self.current_admin = nil
end

def redirect_back_or(default)
  redirect_to(adminsession[:return_to] || default)
  clear_return_to
end

def deny_access
  store_location
 redirect_to login_path, :notice => "Please log in to access this page."
end

private

  def store_location
    adminsession[:return_to] = request.fullpath
  end

  def clear_return_to
    adminsession[:return_to] = nil
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:24:10+00:00Added an answer on May 31, 2026 at 6:24 pm

    All helpers are (by default) mixed in and available in all controllers. Looks like the methods you are using should be protected or private members of your controllers instead. You can make them helper methods to be available in your views, i.e. helper_method :signed_in?.

    Personally I never liked the lack of namespacing with helpers anyway. I like the presenter pattern much better (see RailsCasts Pro].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a Wordpress MU admin plugin, that will insert some
I am trying to create a module for an admin interface but am having
I am trying to create a page in my WordPress Admin Area that displays
I am trying to create a system that enables the admin to upload a
I'm trying to create a new admin view that: 1. has a header of
I'm in psql trying to create a table for a python admin. It's actually
I am trying to create a search page, this allows the admin to search
I'm trying to create a program in C# that lists the content of a
I'm trying to create an admin backend for a cms. So I have a
I am trying to create an application that uses <authorization> rules to limit the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.