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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:17:10+00:00 2026-06-07T05:17:10+00:00

Here is my code in the view: show.html.erb <ul> <% @bullets.each do |r| %>

  • 0

Here is my code in the view: show.html.erb

<ul>
<% @bullets.each do |r| %>
    <li><%= r.content %></li>
<% end %>
</ul>

Here is my code in the controller: users_controller.rb

if cookies[:bullets].nil?
  @bullets = Bullet.all.shuffle.first(4)
  cookies[:bullets] = @bullets.collect(&:id)
else
 @bullets = []
 cookies[:bullets].each do |id|
   @bullets << Bullet.find(id)
 end
end

This returns undefined method ‘each’ for nil:NilClass on

<% @bullets.each do |r| %>

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).

EDIT: This is the ENTIRE controller:

class StudentsController < 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 => 'app/views/success'
      else 
        render :file => '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(&:id)
    else
     @bullets = []
     cookies[:bullets].each do |id|
       @bullets << 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 

EDIT #2: Like so?

#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(&:id)
    else
     @bullets = []
     cookies[:bullets].each do |id|
       @bullets << Bullet.find(id)
     end
    end

           respond_to do |format|
         format.html # show.html.erb
         format.json { render json: @student } 
           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-07T05:17:11+00:00Added an answer on June 7, 2026 at 5:17 am

    Looks like it should be:

    #GET /students/{:id}
    def show 
    
       @student = Student.find_by_url(params[:id])
    
       #BULLETS Randomizing /students/new.html.erb
       if cookies[:bullets].nil?
         @bullets = Bullet.all.shuffle.first(4)
         cookies[:bullets] = @bullets.collect(&:id)
       else
         # simpler to use an 'in list' for only 4 id's
         Bullet.where("id in (?)", cookies[:bullets])
       end
    
       respond_to do |format|
         format.html # show.html.erb
         format.json { render json: @student } 
       end  
    end
    

    Notice I converted your loop over the cookies array into a single statement with an ‘in list’ — that should simplify the sql generated for the lookups.

    Although, it’s arguable that this code should be pushed into the model:

    class Bullet < ActiveRecord::Base
    
      NUM_USER_BULLETS = 4
      # fetch a random set of 
      def self.user_bullets
        Bullet.all.shuffle.first(NUM_USER_BULLETS)
      end
    end
    

    Or something similar. Then your controller is simpler:

    #GET /students/{:id}
    def show 
    
       @student = Student.find_by_url(params[:id])
    
       #BULLETS Randomizing /students/new.html.erb
       if cookies[:bullets].nil?
         @bullets = Bullet.user_bullets
         cookies[:bullets] = @bullets.collect(&:id)
       else
         # simpler to use an 'in list' for only 4 id's
         Bullet.where("id in (?)", cookies[:bullets])
       end
    
       respond_to do |format|
         format.html # show.html.erb
         format.json { render json: @student } 
       end  
    end
    

    With the code migrated into your model, your controller is simpler.

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

Sidebar

Related Questions

here the following code is used to view the present modal view controller. [[self
here is my code in Controller var q = context.post; return View(q); in view
Here is ALL my code for this specific view controller. As the title of
Here is code on view in FeesController show action template: <div id=payers_controls> <%= link_to_remote('New
you can view full source code here dpaste.com/hold/167199 Error: delete() takes exactly 2 arguments
[QueryInterceptor(Somethings)] public Expression<Func<Something, bool>> OnSomethings() { // Code here } I had a view
Here's the code I'm using to make a custom AlertDialog. public void onButtonClicked(View v)
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
i wanna do a single selection in my table view here is my code
The is my code.I am here pupulating a list view with some array data.when

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.