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?
In rails 3, you should use <%=
instead of <%
for form_for
and fields_for
. See if that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With