On Apr 25, 11:59 am, Colin Law <clan...@googlemail.com> wrote:
> On 25 April 2011 16:50, amrit pal pathak <amritpalpath...@gmail.com> wrote:
>
>
>
> > On Apr 25, 9:37 am, Tim Shaffer <timshaf...@me.com> 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
>
> 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.
                   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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to