Příklad 47.10. projects_controller.rb
def new @project = Project.new @project.tasks.build # vytvoří jeden nový úkol end def create @project = Project.new(params[:project]) params[:tasks].each_value { |task| @project.tasks.build(task) } if @project.save redirect_to :action => 'new' else render :action => 'new' end end def add_task @task = Task.new end
Příklad 47.11. projects/new.rhtml
<% form_for :project, :url => { :action => 'create' } do |f| %> <p>Name: <%= f.text_field :name %></p> <h2>Tasks</h2> <div id="tasks"> <% @project.tasks.each_with_index do |task, index| %> <%= render :partial => 'task_fields', :locals => { :task => task, :index => index } %> <% end %> </div> <%= render :partial => 'add_task_link', :locals => { :index => @project.tasks.size } %> <p><%= submit_tag 'Create Project' %></p> <% end %>
Příklad 47.12. projects/_task_fields.rhtml
<div id="task_<%= index %>"> <% fileds_for "tasks[#{index}]", task do |f| %> <p><%= f.text_field :name %> <%= link_to_remote 'remove', :url => { :action => 'remove_task', :index => index } %> </p> <% end %> </div>
Příklad 47.13. projects/add_task.rjs
page.insert_html :bottom, :tasks, :partial => 'task_fields', :locals => { :task => @task, :index => params[:index] }