Note that there are some explanatory texts on larger screens.

plurals
  1. POReturns undefined method 'each' for nil:NilClass?
    primarykey
    data
    text
    <p>Here is my code in the view: show.html.erb</p> <pre><code>&lt;ul&gt; &lt;% @bullets.each do |r| %&gt; &lt;li&gt;&lt;%= r.content %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; </code></pre> <p>Here is my code in the controller: users_controller.rb</p> <pre><code>if cookies[:bullets].nil? @bullets = Bullet.all.shuffle.first(4) cookies[:bullets] = @bullets.collect(&amp;:id) else @bullets = [] cookies[:bullets].each do |id| @bullets &lt;&lt; Bullet.find(id) end end </code></pre> <p>This returns undefined method 'each' for nil:NilClass on </p> <pre><code>&lt;% @bullets.each do |r| %&gt; </code></pre> <p>I would like to know why it does this, and how can I fix it to post four random fixed bullet contents from a database (sqlite3) table named "bullets" (column is content).</p> <p>EDIT: This is the ENTIRE controller:</p> <pre><code>class StudentsController &lt; ApplicationController #GET / def index @students = Student.all respond_to do |format| format.html # index.html.erb format.json { render json: @students } end end #GET /new def new @student = Student.new end #POST def create @student = Student.new(params[:student]) if @student.save render :file =&gt; 'app/views/success' else render :file =&gt; 'app/views/students/fail' end end #GET /students/{:id} def show @student = Student.find_by_url(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @student } end end #BULLETS Randomizing /students/new.html.erb if cookies[:bullets].nil? @bullets = Bullet.all.shuffle.first(4) cookies[:bullets] = @bullets.collect(&amp;:id) else @bullets = [] cookies[:bullets].each do |id| @bullets &lt;&lt; Bullet.find(id) end end #GET /students/1/edit def edit @student = Student.find_by_url(params[:id]) end def update @student = Student.find_by_url(params[:id]) respond_to do |format| if @student.update_attributes(params[:student]) format.html { redirect_to @student, notice: 'Student was successfully updated.'} else format.html { render action: "edit" } format.json { render json: @student.errors, status: :unprocessable_entity } end end end #DELETE def destroy @student = Student.find_by_url(params[:id]) @student.destroy respond_to do |format| format.html { redirect_to students_url } format.json { head :no_content } end end end </code></pre> <p>EDIT #2: Like so?</p> <pre><code>#GET /students/{:id} def show @student = Student.find_by_url(params[:id]) #BULLETS Randomizing /students/show.html.erb if cookies[:bullets].nil? @bullets = Bullet.all.shuffle.first(4) cookies[:bullets] = @bullets.collect(&amp;:id) else @bullets = [] cookies[:bullets].each do |id| @bullets &lt;&lt; Bullet.find(id) end end respond_to do |format| format.html # show.html.erb format.json { render json: @student } 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.
 

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