入门锦集1-8都来自官方的翻译:http://edgeguides.rubyonrails.org/getting_started.html
1. 构建多模型表单
现在的博客一般都有便签功能, 方便读者通过关键字索引文章. 要实现该特性你的应用需要在一个表单中交互多个模型. 那么这时候Rails提供了另一个重要的功能: 嵌套表单
为了展示, 我们将为post的多标签提供支持. 首先, 我们需要为标签创建模型:
$ rails g model tag name:string post:references
然后:
$ rake db:migrate
接下来, 编辑post.rb建立另一半关联, 并告诉rails你需要通过posts来编辑tags:
class Post < ActiveRecord::Base validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } has_many :comments, :dependent => :destroy has_many :tags #实际上对于标签来说应该是一个多对多的关联, 为了方便教程我在这里使用一对多关系 accepts_nested_attributes_for :tags, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } end
:allow_destroy 告诉rails在视图上显示一个移除复选框, 而 :reject_if 将会阻止任何空属性的标签的保存.
现在我们修改 views/posts/_form.erb 模板来加入tag:
<% @post.tags.build %> <%= form_for(@post) do |post_form| %> <% if @post.errors.any? %><% end %><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:
<% @post.errors.full_messages.each do |msg| %>
- <%= msg %>
<% end %><%= post_form.label :name %>
<%= post_form.text_field :name %><%= post_form.label :title %>
<%= post_form.text_field :title %><%= post_form.label :content %>
<%= post_form.text_area :content %>Tags
<%= render :partial => "tags/form", :locals => {:form => post_form} %><%= post_form.submit %><% end %>
现在我们把 form_for @post do |f| 改为 |psot_form| 使之更容易理解.
本示例显示了 render方法的另一个参数 :locals 它可以传递局部变量, 在这里我们希望 form 变量映射为 post_form 对象.
同时我们还在开始处初始化一个tag对象. (试试取消该行会发生什么)
现在创建文件夹 views/tags/ 然后在里面添加一个 _form.erb 局部模板:
<%= form.fields_for :tags do |tag_form| %><%= tag_form.label :name, 'Tag:' %> <%= tag_form.text_field :name %><% unless tag_form.object.nil? || tag_form.object.new_record? %><%= tag_form.label :_destroy, 'Remove:' %> <%= tag_form.check_box :_destroy %><% end %> <% end %>
同样, 请留意下tags表单生成的html代码.
最后, 我们编辑show.html.erb来显示这些tags:
<%= notice %>
Name: <%= @post.name %>
Title: <%= @post.title %>
Content: <%= @post.content %>
Tags: <%= @post.tags.map { |t| t.name }.join(", ") %>
Comments
<%= render @post.comments %>Add a comment:
<%= render "comments/form" %> <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> |
现在你可以直接在创建或编辑post页面中添加或删除tags了.
不过 @post.tags.map { |t| t.name }.join(", ") 感觉不是那么友好, 我们可以通过帮助器来改变.
2. 视图帮助器
视图帮助器位于 app/helpers, 可以提供一些可重用的视图代码片段. 在这里, 我们需要把所有tags的名称连起来并用逗号隔开, 由于它位于Posts视图, 那么我们可以在 PostsHelper 中实现:
module PostsHelper def join_tags(post) post.tags.map { |t| t.name }.join(", ") end end
然后把上面的 <%= @post.tags.map { |t| t.name }.join(", ") %> 改为:
<%= join_tags @post %>
转自: http://onia.iteye.com/blog/833152