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? %>


<%= pluralize(@post.errors.count, “error”) %> prohibited
this post from being saved:

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

<% end %>

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit %>
<% end %>

Any clue?
Thanks

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

On Apr 25, 9:01am, Tim S. [email protected] wrote:

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

On Apr 25, 9:37am, Tim S. [email protected] wrote:

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

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?

On 25 April 2011 16:50, amrit pal pathak [email protected]
wrote:

Showing /home/amrit/boook/app/views/book/_form.html.erb where line #1
raised:
undefined method `model_name’ for NilClass:Class

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

On Apr 25, 11:59am, Colin L. [email protected] wrote:

same error exits

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

Books are coming soon!

Listing Books

<% @book.each do |book| %> <% end %>
Title Summary
<%= book.title %> <%= book.content %>

<%= link_to 'New book', new_book_path%>

2)new.html.erb:

New post

<%= render 'form' %>

3)_form.html.erb

<%= form_for(@book) do |f| %>
<% if @book.errors.any? %>


<%= pluralize(@book.errors.count, “error”) %> prohibited
this book from being saved:



    <% @book.errors.full_messages.each do |msg| %>
  • <%= msg %>

  • <% end %>


<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit %>
<% 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

On 26 April 2011 08:56, amrit pal pathak [email protected]
wrote:


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.

Title Summary <% @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.

On Apr 26, 4:15am, Colin L. [email protected] wrote:

1)Index.html.erb:

<% @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| %>

<%= book.title %> <%= book.content %> <% end %>

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

On 26 April 2011 09:23, amrit pal pathak [email protected]
wrote:

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

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

On 26 April 2011 09:15, Colin L. [email protected] wrote:

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

You have the patience of a saint…

On 26 April 2011 09:41, amrit pal pathak [email protected]
wrote:

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.

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

On Apr 26, 4:30am, amrit pal pathak [email protected]
wrote:

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??

On Apr 26, 4:46am, Michael P. [email protected] wrote:

On 26 April 2011 09:15, Colin L. [email protected] wrote:

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

You have the patience of a saint…

Thanks sir

On Apr 26, 4:44am, Colin L. [email protected] wrote:

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

[email protected]:~/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.

On Apr 26, 5:09am, Colin L. [email protected] wrote:

the code?
I used the rails console it says

[email protected]:~/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.
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

On 26 April 2011 09:59, amrit pal pathak [email protected]
wrote:

3000/book ,but when i click on “New book” link it gave follwoing 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

[email protected]:~/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

On Apr 26, 10:21am, amrit pal pathak [email protected]
wrote:

          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

On Apr 26, 5:55am, Frederick C. [email protected]
wrote:

@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

Books are coming soon!

Listing Books

Title Summary

<%= 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs