NoMethodError in Book

amritpal p. wrote in post #995086:

Stuff…

You seem to be lacking some fundamental “organizational” knowledge as it
pertains to a Rails application.

What I found very instructive way back when was to scaffold a basic
application and see what Rails created, and how it was organized. Then
I fiddled with that scaffolded application, one change at a time.

If you have an existing application, you can just add a scaffolded
model, controller, and views via:

rails g scaffold person first_name:string last_name:string

(doesn’t have to be person, could be anything, just give it a model name
and some attributes with types).

Then look at the controller, model, and views that are created. Most
enlightening.

On 26 Apr 2011, at 12:28, amrit pal pathak [email protected]
wrote:

it is defined in controller as :
click on “New book”,it gives the error
Yes, we know that. What you don’t seem to grasp is that because the new
page is being displayed the index method isn’t called (the new method,
if it exists will be called) so @book isn’t set

Fred

On Apr 26, 7:52am, Ar Chron [email protected] wrote:

If you have an existing application, you can just add a scaffolded
model, controller, and views via:

rails g scaffold person first_name:string last_name:string

(doesn’t have to be person, could be anything, just give it a model name
and some attributes with types).

Then look at the controller, model, and views that are created. Most
enlightening.
Yes i have tried scafffolding.But i want to generate a form
without scaffold.i want to manualy create a controller,view and model
for it.please help to do it.

Thanks

On Apr 26, 7:53am, Frederick C. [email protected]
wrote:

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

Yes, we know that. What you don’t seem to grasp is that because the new page is
being displayed the index method isn’t called (the new method, if it exists will
be called) so @book isn’t set

     you mean i should make a  another method "new" inside

controller and “@book =Book.new” should be written there?

Amrit pal

On Apr 26, 1:35pm, amrit pal pathak [email protected]
wrote:

you mean i should make a another method "new" inside

controller and “@book =Book.new” should be written there?

Yes. Seriously, give the scaffolds a go, even if you don’t want to use
them for your finished product (which is fair enough), you’d at least
get a vague idea of what goes where and what things need to be done.

Fred

amritpal p. wrote in post #995096:

Yes i have tried scafffolding.But i want to generate a form
without scaffold.i want to manualy create a controller,view and model
for it.please help to do it.

Thanks

Yes, fair enough. But use the scaffolded elements as the prototype for
your manual creations… If you have scaffolded something, then the
“right way” (or at least the way that follows the Rails conventions most
cleanly) is there in front of you.

Suppose you want a book model.

  1. You’ll need a books_controller. By conventions:
  • the index method in the controller retrieves all the books into a
    variable named @books.
  • the show method retrieves a single existing book stored into a
    variable named @book according to an id received in the params
  • the new method created a new book, named @book via a simple @book =
    Book.new
  • the edit method also retrieves a single existing book stored into a
    variable named @book according to an id received in the params
  • the create method receives a book definition via params and creates a
    new @book instance from the values contained in the params, then
    attempts to persist that object in the DB.
  • the update method retrieves an existing book via a find using the id
    received in the params, then updates that books attributes with those
    also received in the params.
  • destroy finds a book according to the id received in the params, and,
    well, deletes it.

Each of these controller methods, be default, will have its results
rendered in a view named according to the method that the view supports.

  • index method renders its output via the index.html.erb
  • new and edit render via new.html.erb and edit.html.erb, each of which
    uses a partial named _form.html.erb (and yes, that underscore at the
    beginning of the filename is important).

If you are creating your own, then don’t forget to update the routes.rb
when you add a new model and controller, or action to an existing
model/controller.

All of this is readily apparent from a study of a scaffolded solution,
and a little reading.

Awaiting your helpful reply!!

amritpal p. wrote in post #995333:

Awaiting your helpful reply!!

You’ve already got an answer - your ‘@book’ method is undefined in the
‘new’ action of your controller. You need to define ‘@book’ in ‘new’
before you can access it.

When an object such as a variable is undefined, it will return nil which
is what you’re seeing.

On Apr 26, 8:35am, amrit pal pathak [email protected]
wrote:

class BookController < ApplicationController
Yes, we know that. What you don’t seem to grasp is that because the new page
is being displayed the index method isn’t called (the new method, if it exists
will be called) so @book isn’t set

you mean i should make a another method "new" inside

controller and “@book =Book.new” should be written there?

Awaiting your helpful reply!!

On Apr 26, 8:35am, amrit pal pathak [email protected]
wrote:

class BookController < ApplicationController
Yes, we know that. What you don’t seem to grasp is that because the new page
is being displayed the index method isn’t called (the new method, if it exists
will be called) so @book isn’t set

you mean i should make a another method "new" inside

controller and “@book =Book.new” should be written there?
Awaiting your helpful reply!!