multimodel_forms 插件用法

简介:
在两个model之间关系为1对多时,希望用于输入的表单的样子是这样:
其中的add_task和delete是js效果。下面以实例来说明如何使用这个插件
 
1.rails demo -d mysql
2.mysqladmin -u root create demo_development
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 
===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 
 
模型:
class Project < ActiveRecord::Base 
    has_many :tasks, :dependent=>:destroy 
    has_many_with_attributes :tasks, :dependent=>:destroy 
end 
class Task < ActiveRecord::Base 
    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> 
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 %> 
在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>
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%>
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>
现在启动服务, 看看效果吧!




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/106742,如需转载请自行联系原作者
目录
相关文章
VScode使用element-ui插件准备
VScode使用element-ui插件准备
1847 1
VScode使用element-ui插件准备
Sublime Text自定义代码片段Code Snippets
Sublime Text自定义代码片段Code Snippets
281 0
|
Web App开发
【视频】自然框架之分页控件的使用方法(二) 下载、DLL说明和web.config的设置
    上次说的是QuickPager分页控件的PostBack的使用方式,也提供了源码下载。但是有些人下载之后发现有一大堆的文件夹,还有一大堆的DLL,到底要用哪个呀?不会都要用吧。     当然不需要全都引用了,只需要引用三个DLL就可以了。
1105 0
|
Web App开发 前端开发 索引