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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:49:36+00:00 2026-06-17T05:49:36+00:00

I seem to be unable to capture a nil response from my api call,

  • 0

I seem to be unable to capture a nil response from my api call, when there are no results I get this error

undefined method `[]' for nil:NilClass

So if this happens I would like to show a message like ‘No book found’, so i have tried these

 <% if @results.empty? %>
 <% if @results.nil? %>
 <% if @results.nil %>

but none of them capture the error

Controller

def results
results = book_search(params[:search])
@results = results
@book = Book.new
@book.author = results.first["artistName"]
@book.bookname = results.first["trackName"]
@book.image = results.first["artworkUrl100"].gsub("100x100-75.jpg", "200x200-75.jpg")
@book.description = results.first["description"]
end

View

<div class="container">
 <div class="row">
  <div class="span8 offset2">
   <% if @results.nil? %>
    <h1 class="resultTitle">sorry we can't find anything for that book.</h1>
     <%= link_to "Back To Search", root_path, :class => 'backButton' %>
   <% end %>


  <%= @results.first["trackName"] %> <br>
  <img src =<%= @results.first["artworkUrl100"] %>> <br>
  <%= @results.first["artistName"] %> <br>
  <%= truncate(remove_tags(@results.first["description"]), :length => 250) %> <br>

  <%= form_for @book do |f| %>
  <%= f.label :category_id, "Category" %>
  <%= f.collection_select(:category_id, Category.all, :id, :name, :prompt => 'Please select a Category') %>
  <%= f.hidden_field :author %>
  <%= f.hidden_field :bookname %>
  <%= f.hidden_field :image %>
  <%= f.hidden_field :description %>

   <%= f.submit 'Save book' %>
  <% end %>
 </div>
 </div>
 </div>

Any ideas on what it could be

Thanks

  • 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-17T05:49:37+00:00Added an answer on June 17, 2026 at 5:49 am

    The latter version will return true if @results is nil. You have some other error elsewhere.

    If the error still shows up, then you access results even when @results.nil? returns true. More specifically take a look at the places where you call the square bracket operator [] as the error is caused by one of them.

    EDIT: after discussion in chat we have discovered the problem. First in the conroller book_search returns an empty array and then, when invoking results.first["artistName"] this causes the error, because results.first is nil. So to fix that we added the following to the controller:

      def results
        results = book_search(params[:search])
        @results = results 
        unless @results.empty?
          @book = Book.new 
          @book.author = results.first["artistName"] 
          @book.bookname = results.first["trackName"] 
          @book.image = results.first["artworkUrl100"].gsub("100x100-75.jpg", "200x200-75.jpg") 
          @book.description = results.first["description"] 
        end
      end
    

    I.e. a check to only create a new book and set its properties if a book was found. Now in the view, a check should be added to only render the rest of the view(the part that invokes [] operator on @results if results is not empty. This is achieved using an else statement:

     <div class="container">
     <div class="row">
      <div class="span8 offset2">
       <% if @results.empty? %>
        <h1 class="resultTitle">sorry we can't find anything for that book.</h1>
         <%= link_to "Back To Search", root_path, :class => 'backButton' %>
       <% else %>  <--- I've putted an else instead of end here!!!
    
    
        <%= @results.first["trackName"] %> <br>
        <img src =<%= @results.first["artworkUrl100"] %>> <br>
        <%= @results.first["artistName"] %> <br>
        <%= truncate(remove_tags(@results.first["description"]), :length => 250) %> <br>
    
        <%= form_for @book do |f| %>
        <%= f.label :category_id, "Category" %>
        <%= f.collection_select(:category_id, Category.all, :id, :name, :prompt => 'Please select a Category') %>
        <%= f.hidden_field :author %>
        <%= f.hidden_field :bookname %>
        <%= f.hidden_field :image %>
        <%= f.hidden_field :description %>
    
         <%= f.submit 'Save book' %>
        <% end %>
      <%end %> <--- Added an end for the if-else here.
     </div>
     </div>
     </div>
    

    So I have added an else and an end that I have indicated in the code. Why do you need to do that? Well because even after performing the check and displaying the message, the rest of the page was still evaluated and this bit: %= @results.first["trackName"] %> <br> was causing the error. You should not try to call the [] operator if you know @results is empty. The new version only displays the remaining part of the page in the else case i.e. if @results is not empty.

    Hope this makes it clear.

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

Sidebar

Related Questions

I guess this is an easy question but I seem to be unable to
As already said by the headline, I get a compile error I seem to
I seem completely unable to solve this problem. When I run this loop: for
Excuse me if my question may seem inappropriate but i was unable to find
Seem to run into a service endpoint not found problem when trying to get
I seem to be a little lost on what to so about this, im
I seem to have a problem passing some strings on from one form to
Ok, I've searched quite a bit, but seem unable to find an answer or
I seem to be unable to change the colour of the DetailTextLabel in a
I delved into C# but came across a problem that I seem unable to

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.