Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fields_for not rendering - rails 3

Finally moved to Rails 3 for a new project and already running into a rookie problem.

Trying to do a simple nested form.

2 models: list and tasks

List model

class List < ActiveRecord::Base
  has_many :tasks, :dependent=>:destroy
  accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:name].blank? }
end

Task Model

class Task < ActiveRecord::Base
  belongs_to :list

end

List Controller

def new
   @list = List.new
   3.times{ @list.tasks.build }
end

lists/new.html.erb

<% form_for :list, :url=>{:action=>"create"} do |f| %>
    <%= f.text_field :name, :class=>'big' %>
    <%= f.label :name, "ex: Today's Todos, Christmas List" %>

    <% f.fields_for :tasks do |builder| %>
        <p>
            <%= builder.label :name, "Task" %>
            <%= builder.text_field :name %>
            <%= builder.check_box :_destroy %>
        </p>
    <% end %>

    <p><%= submit_tag "Create List", :id => "submit", :name => "submit", :class => "form_submit", :disabled => false, :disable_with => "Please wait..." %></p>

<% end -%>

Using debug @list I can see the 3 tasks I created in my controller, but it fields_for won't render.

Any ideas?

like image 494
Ryan Avatar asked Dec 11 '10 02:12

Ryan


1 Answers

In rails 3, you should use <%= instead of <% for form_for and fields_for . See if that helps.

like image 87
bowsersenior Avatar answered Sep 18 '22 20:09

bowsersenior