partials issue

I'm following an example in the "Head First Rails" book regarding partials.

The book seems to be using a version prior Rails 3.0.0, which I'm using.

I'm having a small issue here.

There is a "new.html.erb" for "Seats" which will be created as a partial to be included in the "show.html.erb" page of "flights".

So, the book mentions to copy: app/views/seats/new.html.erb and paste it to app/views/flights/_new_seat.html.erb

But, I'm using Rails 3.0.0, and you know that "new.html.erb" for "Seats" looks something like this:

<h1>New seat</h1>

<%= render 'form' %>

<%= link_to 'Back', seats_path %>

So, there is a _form.html.erb created automatically.

What did I do?

I copied the code above and inserted it as required in app/views/flights/_new_seat.html.erb and removing the last line.

Now came the problem when the book mentions that we make @seat in app/views/flights/_new_seat.html.erb a LOCAL variable, as follows:

<% form_for(seat) do | f| % >

But, where is: <% form_for(@seat) do | f| % > in my case?

It is in _form.html.erb

And, here is a part of _form.html.erb (Notice @seat)

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

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

If I change @seat here to "seat", this I think will affect "new.html.erb" and "edit.html.erb" of "seats".

So, I copied the file to app/views/flights/_form_seat.html.erb and changed ALL @seat to "seat".

After running the application I get the following:

undefined local variable or method `seat' for #<#<Class:0x4de6b78>:0x4e5b9b0>

Extracted source (around line #1):

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

How can I solve this issue?

Sorry for my long question, but had to clarify the point.

Thanks a lot.

I forgot to mention:

This is how I made the render in app/views/flights/_new_seat.html.erb

<%= render 'form_seat', :locals => {:seat => Seat.new} %>

Provided again, I'm using Rails 3.0.0

Thanks.

pass the seat variable with your render call something like this,

<%= render 'form', :locals => { :seat => @seat } %>

Veera Sundaravel wrote:

pass the seat variable with your render call something like this,

<%= render 'form', :locals => { :seat => @seat } %>

-- Regards,

T.Veerasundaravel. Veerasundaravel's Ruby on Rails Weblog | Ruby on Rails Weblog for Ebooks, Ruby Tips, Rails Tips and more. @veerasundaravel

Thanks @Veera.

It actually worked when I inserted :partial => explicitly like this:

<%= render :partial => 'form_seat', :locals => {:seat => Seat.new } %>

When I tried your solution:

<%= render 'form_seat', :locals => {:seat => @seat } %>

I got the following:

undefined local variable or method `seat' for #<#<Class:0x515bb78>:0x515aa50>

Extracted source (around line #1):

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

Thanks.

Try using <%= render 'form', :seat => @seat %> without locals.

tundrax wrote:

Try using <%= render 'form', :seat => @seat %> without locals.

Thanks @tundrax. I'll try that soon.

Abder-Rahman Ali wrote:

I'm following an example in the "Head First Rails" book regarding partials.

The book seems to be using a version prior Rails 3.0.0, which I'm using.

[...]

Then you're setting yourself up for trouble. If you're learning Rails 3, find a Rails 3 book.

Best,

@Marnen. The issue was solved.

Thanks.