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

  • SEARCH
  • Home
  • 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 6001799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:53:10+00:00 2026-05-23T00:53:10+00:00

i am using devise to register and authenticate users.What i am trying to acomplish

  • 0

i am using devise to register and authenticate users.What i am trying to acomplish is for a user to sign in and be able to add a book in his profile.And later when he sign back in the book will be shown.So in my application i have user model and book model.But i am having a problem accessing user in one of the controller.I am getting a exception undefined method `books’ for nil:NilClass.

VIEW

Home#index

   <p >Welcome to the new generation</p>
    <p><%= link_to "Add new Book",:controller =>"book", :action => 'new' %></p>
   <% @books.each do |b| %>
  <p><%= b.author%></p>
 <p><%= b.title%></p>
 <%end%>

HOME CONTROLLER

class HomeController < ApplicationController
 def index
 @user = current_user
 @user.books||=Book.new
 @books=@user.books
end
end

BOOK CONTROLLER

class BookController < ApplicationController
 def new
@books = Book.new
# redirect_to :controller=>"home" ,:action=>"index"
end

 def create
 @books = Book.new(params[:book])
  if @books.save
    render "home/index"
       #redirect_to :controller=>"home" ,:action=>"index"
  else

        render :action => 'new'
  end
end

BOOK VIEW

<h1>Book#new</h1>

 <%= form_for(:book) do |f| %>
  <p><%= f.text_field :title %></p>
  <p><%= f.text_field :author %></p>
  <p><%= f.submit "Add book"%> 

BOOK MODEL

class Book < ActiveRecord::Base
belongs_to :user
end

USER MODEL

class User < ActiveRecord::Base
has_many :books
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password,         :password_confirmation,:firstname,:lastname,:school,:major,:sex,:zipcode


 end

ROUTE

 Campus::Application.routes.draw do


 get "book/index"

 get "book/edit"

  get "book/new"

  get "home/edit"

  devise_for :users
  resources :book     
  root :to=> "home#index"
  match '/book/new' =>"home#index"
   end

CREATE TABLE

class CreateBooks < ActiveRecord::Migration
  def self.up
create_table :books do |t|
  t.text :title
  t.text :author
  t.integer :user_id, :null => false
  t.timestamps
 end
   add_foreign_key(:books, :users)
 end

def self.down
drop_table :books
remove_foreign_key(:books, :users)
end
end

DB SCHEMA

  ActiveRecord::Schema.define(:version => 20110609055608) do

  create_table "books", :force => true do |t|
  t.text     "title"
  t.text     "author"
  t.datetime "created_at"
  t.datetime "updated_at"
  end

  create_table "courses", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  end

  create_table "strong_ins", :force => true do |t|
  t.string   "subject"
  t.string   "topic"
  t.text     "description"
  t.datetime "created_at"
  t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
  t.string   "email",                               :default => "", :null => false
  t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
  t.string   "password_salt",                       :default => "", :null => false
  t.string   "reset_password_token"
  t.string   "remember_token"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",                       :default => 0
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "firstname"
  t.string   "lastname"
  t.text     "school"
  t.text     "major"
  t.string   "sex"
  t.integer  "zipcode"
 end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name =>      "index_users_on_reset_password_token", :unique => true

  create_table "weak_ins", :force => true do |t|
   t.string   "subject"
   t.string   "topic"
   t.text     "description"
   t.datetime "created_at"
   t.datetime "updated_at"
   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-23T00:53:11+00:00Added an answer on May 23, 2026 at 12:53 am

    To get the currently logged in user in controllers using devise you simply use the current_user variable. Also, the relationship between User and Book is has_many :books so current_user.book doesn’t exist.

    class HomeController < ApplicationController
     def index
      @user = current_user
      @books = @user.books
     end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using OmniAuth + Devise to allow users to register using Facebook/Twitter/Gowalla/etc attached to
Iam Trying to create authentications using devise with riak as the database. I found
I am using Omniauth and Devise. Users using gmail or FB can easily create
I'm using devise and I put the login and sign up forms in the
So I'm making an app where I want the users to be able add,
I am using devise to manage user authentication in my rails app. Devise is
I'm using Devise for user registration. It automatically gives me the form for a
I'm using devise to do the new user registration. Right after a new user
I am using Devise with rails and i want to add a method getAllComments,
I am trying to talk to a device using python. I have been handed

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.