Note that there are some explanatory texts on larger screens.

plurals
  1. PORails says class is nil despite console showing database
    primarykey
    data
    text
    <p>I have a database of objects (tools) in my Ruby on Rails project. When I use "rails dbconsole" and</p> <pre><code>select * from tools; </code></pre> <p>it returns a whole list of tool objects. But when I try to view the following page, it gives me an error. </p> <p>Page:</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;%= stylesheet_link_tag "tools", :media =&gt; "all" %&gt; &lt;body&gt; &lt;%= @tools.each do |tool| %&gt; &lt;%= link_to(tool(image_tag.image_url)) %&gt; &lt;% end %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Error: </p> <pre><code>undefined method `each' for nil:NilClass </code></pre> <p>When I change the code to add an if statement against nil objects, the page works (without displaying any tools).</p> <pre><code>&lt;% if @tools.nil? %&gt; &lt;% else %&gt; &lt;%= @tools.each do |tool| %&gt; &lt;%= link_to(tool(image_tag.image_url)) %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>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! </p> <p>EDIT: Added tools_controller.rb</p> <pre><code>class ToolsController &lt; 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 =&gt; @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 =&gt; 'tool was successfully created.' } format.json { render :json =&gt; @tool, :status =&gt; :created, :location =&gt; @tool } else format.html { render :action =&gt; "new" } format.json { render :json =&gt; @tool.errors, :status =&gt; :unprocessable_entity } end end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload