NoMethodError in Book

I got a following error.

NoMethodError in Book#new

Showing /home/amrit/boook/app/views/book/_form.html.erb where line #1
raised:

undefined method `model_name' for NilClass:Class

The content of _form.html.erb file are:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Any clue?
Thanks

What does your controller look like? Do you define the @post variable?

Yes i defined it.it looks like
  class BookController < ApplicationController
  def index
  @book =Book.all
  render :index
  end
  end

Thanks

Actually it looks like you’re not defining it in the example you provided.

Your controller is defining a variable called @book, but your view is trying to reference a variable called @post. Do you have a PostsController?

Yes it was mistake,but now i changed @post to @book.Still
same error exits
NoMethodError in Book#new
Showing /home/amrit/boook/app/views/book/_form.html.erb where line #1
raised:
undefined method `model_name' for NilClass:Class

          Thanks

In that case @book is probably nil.

Have a look at the Rails Guide on debugging and it will show you how
to use ruby-debug to break into your code and inspect data and follow
flow. Then you can debug these issues for yourself.

Another useful technique is to open the rails console in your app by
rails console
then you can run code to see what happens. For example you could
enter, in the console
Book.all
and it would show you the result of that.

Note though that if you are using
@book = Book.all
as in an earlier post, that should give you an array of books not a
single one, so formFor(@book) would not be valid. I don't think that
would not give the nil error you are seeing however. You probably
want Book.first or a query to select a particular book.
That assumes you have a book in the database of course. If you want
to create a new one then perhaps
@book = Book,new
is what you want.

Colin

Now i wont want arrary of books for display.I just
want to create a empty form for book so in controller i removed
"@book.Book.all" .Now controller only holds

class BookController < ApplicationController
  def index
@book =Book.new
  end
  end

1)Index.html.erb:

<h1>Books are coming soon!</h1>
<h1>Listing Books</h1>
<table>
  <tr>
    <th>Title</th>
    <th>Summary</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
<% @book.each do |book| %>
  <tr>
    <td><%= book.title %></td>
    <td><%= book.content %></td>
    </tr>
<% end %>
</table>
<br />
<%= link_to 'New book', new_book_path%>

2)new.html.erb:

<h1>New post</h1>
<%= render 'form' %>

3)_form.html.erb

<%= form_for(@book) do |f| %>
  <% if @book.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@book.errors.count, "error") %> prohibited
this book from being saved:</h2>
      <ul>
      <% @book.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Still Error says
NoMethodError in Book#index

Showing /home/amrit/boook/app/views/book/index.html.erb where line #14
raised:

undefined method `each' for #<Book id: nil, created_at: nil,
updated_at: nil>

Thanks colin

...
class BookController < ApplicationController
def index
@book =Book.new

Think about what this is doing. What is @book? It is a new Book
object, not surprisingly.

end
end

1)Index.html.erb:

<h1>Books are coming soon!</h1>
<h1>Listing Books</h1>
<table>
<tr>
<th>Title</th>
<th>Summary</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @book.each do |book| %>

Above is the line the error is on, what is it supposed to be doing?
Please explain what you think it means.

...
NoMethodError in Book#index

Showing /home/amrit/boook/app/views/book/index.html.erb where line #14
raised:

undefined method `each' for #<Book id: nil, created_at: nil,
updated_at: nil>

When you see an error it is not always easy for a beginner to work out
what it means. Please try and work it out for yourself before asking
here. What do you think it might mean? Also as I asked above, what
do you think that line is supposed to do?

Colin

I just hope I am building up rewards for when it is time to meet my Maker.

> ...
> class BookController < ApplicationController
> def index
> @book =Book.new

Think about what this is doing. What is @book? It is a new Book
object, not surprisingly.

> end
> end

> 1)Index.html.erb:

> <h1>Books are coming soon!</h1>
> <h1>Listing Books</h1>
> <table>
> <tr>
> <th>Title</th>
> <th>Summary</th>
> <th></th>
> <th></th>
> <th></th>
> </tr>
> <% @book.each do |book| %>

Above is the line the error is on, what is it supposed to be doing?
Please explain what you think it means.

              I think it retrieves the book details like title and
contents from database.But here it no use so am removing this block.
             <% @book.each do |book| %>
  <tr>
    <td><%= book.title %></td>
    <td><%= book.content %></td>

  </tr>
<% end %>

> NoMethodError in Book#index

> Showing /home/amrit/boook/app/views/book/index.html.erb where line #14
> raised:

> undefined method `each' for #<Book id: nil, created_at: nil,
> updated_at: nil>

When you see an error it is not always easy for a beginner to work out
what it means. Please try and work it out for yourself before asking
here. What do you think it might mean? Also as I asked above, what
do you think that line is supposed to do?

Amrit pal

Afer removing that block(from index.html.erb) ,i can see localhost:
3000/book ,but when i click on "New book" link it gave follwoing error

NoMethodError in Book#new

Showing /home/amrit/boook/app/views/book/_form.html.erb where line #1
raised:

undefined method `model_name' for NilClass:Class

Thanks

> ...
> class BookController < ApplicationController
> def index
> @book =Book.new

Think about what this is doing. What is @book? It is a new Book
object, not surprisingly.

> <% @book.each do |book| %>

Above is the line the error is on, what is it supposed to be doing?
Please explain what you think it means.

         I think it retrieves the book details like title and

contents from database.

How would @book.each retrieve contents from the db? 'each' is a
method of Array (or other collection) that iterates the collection
passing each one to the block. Since @book is not a collection but is
a single Book then each is not a valid method to call.

But here it no use so am removing this block.

Very sensible, though why you are doing this in the index method I do
not know, it looks as if it would be more appropriate in the 'new'
method. I presume you are just experimenting however, which is fine.

Colin

Should creation of model by "rails generate model book"
will resolve this error??

You already have a Book model so no point generating it again.

You have had this error before, please read the error carefully. It
says that something is nil that should not be.
Earlier I suggested using ruby-debug to break into your code to see
what is going on. What do you see if you break in at this point in
the code?

Colin

You have the patience of a saint....

>> Afer removing that block(from index.html.erb) ,i can see localhost:
>> 3000/book ,but when i click on "New book" link it gave follwoing error

>> NoMethodError in Book#new

>> Showing /home/amrit/boook/app/views/book/_form.html.erb where line #1
>> raised:

>> undefined method `model_name' for NilClass:Class
> Should creation of model by "rails generate model book"
> will resolve this error??

You already have a Book model so no point generating it again.

         Yes you are right

You have had this error before, please read the error carefully. It
says that something is nil that should not be.

       Nil means it says db has nothing to show or else??

Earlier I suggested using ruby-debug to break into your code to see
what is going on. What do you see if you break in at this point in
the code?

         I used the rails console it says

amrit@ubuntu:~/boook$ rails console
Loading development environment (Rails 3.0.7)
irb(main):001:0> Book.all
=> []

                        Please let me clear,i am just
expermenting.Please Guides me to just generate a empty form with two
fields (name and content)after click on "New Book " link in the
index.html.erb file(May be without it is connected to db).

Thanks for continuos support.

Thanks sir

>> Afer removing that block(from index.html.erb) ,i can see localhost:
>> 3000/book ,but when i click on "New book" link it gave follwoing error

>> NoMethodError in Book#new

>> Showing /home/amrit/boook/app/views/book/_form.html.erb where line #1
>> raised:

>> undefined method `model_name' for NilClass:Class
> Should creation of model by "rails generate model book"
> will resolve this error??

You already have a Book model so no point generating it again.

    Yes you are right

You have had this error before, please read the error carefully. It
says that something is nil that should not be.

  Nil means it says db has nothing to show or else??

Nothing to do with the database directly, it is saying a variable that
you are using is nil. Look at where you setup the variable that is
nil to find out why.

Earlier I suggested using ruby-debug to break into your code to see
what is going on. What do you see if you break in at this point in
the code?

    I used the rails console it says

amrit@ubuntu:~/boook$ rails console
Loading development environment (Rails 3.0.7)
irb(main):001:0> Book.all
=> []

That is saying that there are no books in the database, but that is
not necessarily anything to do with your problem. As I said earlier
look at where you are setting up the variable that is nil.

Colin

is it saying @book variablei is nil?
   it is defined in controller as :

class BookController < ApplicationController
  def index
  @book =Book.new
  end
  end
                                                One thing more,in
new.html.erb,i have written "render 'form' ",but there is no such file
(view).The file there is _form.html.erb.Is it correct?

Amrit pal

                         is it saying @book variablei is nil?

it is defined in controller as :

class BookController < ApplicationController
def index
@book =Book.new
end
end

Your index action defines @book, but you're testing the "new" action.

                                            One thing more,in

new.html.erb,i have written "render 'form' ",but there is no such file
(view).The file there is _form.html.erb.Is it correct?

That's ok - rails will work that bit out.

Fred

> is it saying @book variablei is nil?
> it is defined in controller as :

> class BookController < ApplicationController
> def index
> @book =Book.new
> end
> end

Your index action defines @book, but you're testing the "new" action.

                Correspond to index action i have index file which
hold following code and i can access this file successfully but when i
click on "New book",it gives the error
<h1>Books are coming soon!</h1>

<h1>Listing Books</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Summary</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</table>
<br />
<%= link_to 'New book', new_book_path%>

> One thing more,in
> new.html.erb,i have written "render 'form' ",but there is no such file
> (view).The file there is _form.html.erb.Is it correct?

That's ok - rails will work that bit out.

      Ok .but if i change the name from _form to form ,is will ok?

Thanks