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 8559041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:56:52+00:00 2026-06-11T15:56:52+00:00

I have a database of objects (tools) in my Ruby on Rails project. When

  • 0

I have a database of objects (tools) in my Ruby on Rails project. When I use “rails dbconsole” and

select * from tools;

it returns a whole list of tool objects. But when I try to view the following page, it gives me an error.

Page:

 <!DOCTYPE html>
<html lang="en">


<%= stylesheet_link_tag "tools", :media => "all" %>
<body>
<%= @tools.each do |tool| %>
 <%= link_to(tool(image_tag.image_url)) %>
 <% end %>

</body>
</html>

Error:

undefined method `each' for nil:NilClass

When I change the code to add an if statement against nil objects, the page works (without displaying any tools).

<% if @tools.nil? %>
<% else %>
  <%= @tools.each do |tool| %>
    <%= link_to(tool(image_tag.image_url)) %>
    <% end %>
<% end %> 

So it seems like @tools doesn’t have any values in it, but when I look at it in the dbconsole, there are tools there. I can’t figure this out, and I’ve spent the past few days googling for answers, so any and all ideas would be welcome!

EDIT: Added tools_controller.rb

class ToolsController < ApplicationController
before_filter :check_authentication
def check_authentication
  unless session[:user_id]
    session[:intended_action] = action_name
    session[:intended_controller] = controller_name
    redirect_to new_session_url
  end
end

  def new
    @tool = Tool.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @tool }
    end
  end

 def show
 end

 def index
    @tools = Tool.all
  end

  # GET /tools/1/edit
  def edit
    @tool = Tool.find(params[:id])
  end

  # POST /tools
  # POST /tools.json
 def create
    @tool = Tool.new(params[:tool])
    respond_to do |format|
      if @tool.save
        format.html { redirect_to @tool, :notice => 'tool was successfully created.' }
        format.json { render :json => @tool, :status => :created, :location => @tool }
      else
        format.html { render :action => "new" }
        format.json { render :json => @tool.errors, :status => :unprocessable_entity }
      end
    end
  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-06-11T15:56:53+00:00Added an answer on June 11, 2026 at 3:56 pm

    In order for the @tools variable to be accessible from your view, you need to declare it in your controller, like this:

    @tools = Tool.all
    

    If you want it to be only accessible from one page, just declare it in the according method.

    Here is an example, assuming you want to make the variable available for your home/index page:

    class HomeController < ApplicationController
    
      def index
         @tools = Tool.all
    
        respond_to do |format|
          format.html # index.html.erb
        end
      end
    end
    

    If you want it to be accessible in all your pages, you can declare it in the before_filter method of your ApplicationController.

    Here is how to do this:

    class ApplicationController < ActionController::Base
      protect_from_forgery
      before_filter :load_variables
    
      # Load variables to be used everywhere
      def load_variables
        @tools = Tool.all
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an adjacency list of objects (rows loaded from SQL database with the
I have a list of ordered objects stored in the database and accessed through
I have a complex network of objects being spawned from a sqlite database using
I currently have a bunch of SQLCHAR objects from a database query. The query
I have a project that needs to create and use COM objects. I found
i have elasticsearch database of geo objects. I want to search the nearest objects
I have a database with lots of game objects, which is being queried by
.Net4 Entity Framework, N-Tier (so objects are detatched) I have 2 objects generated Database
I have a model on top of my database model and map the objects
I'm having trouble with refreshing objects in my database. I have an two PC's

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.