在两个model之间关系为1对多时,希望用于输入的表单的样子是这样:
其中的add_task和delete是js效果。下面以实例来说明如何使用这个插件
1.rails demo -d mysql
2.mysqladmin -u root create demo_development
3.demo> ruby script/plugin install
[url]http://multimodel-forms.googlecode.com/svn/trunk/multimodel_forms[/url]
4.ruby script/generate model project
5.ruby script/generate model task
==001_Migration
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.column :name, :string
end
end
def self.down
drop_table :projects
end
end
def self.up
create_table :projects do |t|
t.column :name, :string
end
end
def self.down
drop_table :projects
end
end
===002_Migration
class CreateTasks < ActiveRecord::Migration
def self.up
create_table :tasks do |t|
t.column :name, :string
t.column :project_id, :integer
end
end
def self.down
drop_table :tasks
end
end
def self.up
create_table :tasks do |t|
t.column :name, :string
t.column :project_id, :integer
end
end
def self.down
drop_table :tasks
end
end
模型:
class Project < ActiveRecord::Base
has_many :tasks, :dependent=>:destroy
has_many_with_attributes :tasks, :dependent=>:destroy
end
has_many :tasks, :dependent=>:destroy
has_many_with_attributes :tasks, :dependent=>:destroy
end
class Task < ActiveRecord::Base
belongs_to :project
end
belongs_to :project
end
ruby script/generate controller=>projects
配置routes
map.resources :projects
在projects_controller中使用make_resourceful(附件中有下载)
class ProjectsController < ApplicationController
make_resourceful do
actions :all
end
end
projects/index页面:
<h1>Listing Projects</h1>
<%@projects.each do |project|%>
<b>Project Name:</b><%= link_to "#{project.name}", project_path(project)%> <%= %>
<%= link_to "edit", edit_project_path(project) %>
<%= link_to "delete", project_path(project), :method=>"delete", :confirm=>"are you sure?" %>
<ul>
<%project.tasks.each do |task|%>
<li>
<%= task.name %>
</li>
<%end%>
</ul>
<%end%>
<p><%= link_to "New Project", new_project_path %></p>
<%@projects.each do |project|%>
<b>Project Name:</b><%= link_to "#{project.name}", project_path(project)%> <%= %>
<%= link_to "edit", edit_project_path(project) %>
<%= link_to "delete", project_path(project), :method=>"delete", :confirm=>"are you sure?" %>
<ul>
<%project.tasks.each do |task|%>
<li>
<%= task.name %>
</li>
<%end%>
</ul>
<%end%>
<p><%= link_to "New Project", new_project_path %></p>
new页面
<%= javascript_include_tag :defaults %>
<h1>New Project</h1>
<%= error_messages_for :project %>
<%form_for(:project, :url=>projects_path) do |f|%>
<p>
<b>Name:</b><br/>
<%= f.text_field(:name) %>
</p>
<h3>Tasks</h3>
<div id="tasks">
<%= render :partial=>"tasks/task", :collection=>@project.tasks %>
</div>
<%= add_link "Add Task", :task %>
<p><%= submit_tag "Create" %></p>
<%end%>
<%= link_to 'back', projects_path %>
<h1>New Project</h1>
<%= error_messages_for :project %>
<%form_for(:project, :url=>projects_path) do |f|%>
<p>
<b>Name:</b><br/>
<%= f.text_field(:name) %>
</p>
<h3>Tasks</h3>
<div id="tasks">
<%= render :partial=>"tasks/task", :collection=>@project.tasks %>
</div>
<%= add_link "Add Task", :task %>
<p><%= submit_tag "Create" %></p>
<%end%>
<%= link_to 'back', projects_path %>
在view下新建一个tasks文件夹,新建一个_task.rhtml局部模板:
<p class="task">
<%fields_for_associated :project , task do |t_form|%>
<%= t_form.text_field :name,:index=>nil %>
<%= delete_link_for(task, "Delete", t_form) %>
<%end%>
</p>
<%fields_for_associated :project , task do |t_form|%>
<%= t_form.text_field :name,:index=>nil %>
<%= delete_link_for(task, "Delete", t_form) %>
<%end%>
</p>
projects/edit.rhtml
<%= javascript_include_tag :defaults %>
<h1>Editing project</h1>
<%= error_messages_for :project %>
<% form_for(:project, :url=>project_path(@project), :html=>{:method=>:put}) do |f|%>
<p>
<b>Name:</b><br/>
<%= f.text_field(:name) %>
</p>
<h3>Tasks</h3>
<div id="tasks">
<%= render :partial=>'tasks/task', :collection=>@project.tasks %>
</div>
<%= add_link "Add Task", :task %>
<p><%= submit_tag "Create" %></p>
<%end%>
<%= error_messages_for :project %>
<% form_for(:project, :url=>project_path(@project), :html=>{:method=>:put}) do |f|%>
<p>
<b>Name:</b><br/>
<%= f.text_field(:name) %>
</p>
<h3>Tasks</h3>
<div id="tasks">
<%= render :partial=>'tasks/task', :collection=>@project.tasks %>
</div>
<%= add_link "Add Task", :task %>
<p><%= submit_tag "Create" %></p>
<%end%>
show.rhtml
<h1><%= @project.name %>Details</h1>
<%= @project.name %> <%= link_to "edit", edit_project_path(@project) %>
<%= link_to "delete", project_path(@project), :method=>"delete", :confirm=>"are you sure?" %>
<ul>
<% @project.tasks.each do |task| %>
<li><%= task.name %></li>
<%end%>
</ul>
<%= @project.name %> <%= link_to "edit", edit_project_path(@project) %>
<%= link_to "delete", project_path(@project), :method=>"delete", :confirm=>"are you sure?" %>
<ul>
<% @project.tasks.each do |task| %>
<li><%= task.name %></li>
<%end%>
</ul>
本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/106742,如需转载请自行联系原作者