fields_for not rendering - rails 3

我只是一个虾纸丫 提交于 2019-11-29 06:13:53

问题


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?


回答1:


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




回答2:


In your list controller in action new you have to add

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


来源:https://stackoverflow.com/questions/4414934/fields-for-not-rendering-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!