`

ruby on rails入门教程之post构建三

 
阅读更多

1. 列出所有Posts

要开始查看功能的最简单的地方应该是列举所有记录的代码了。现在我们打开 app/controllers/post_controller.rb, 看到 index 方法

  1. def  index  
  2.     @posts  = Post.all  
  3.   
  4.     respond_to do  |format|  
  5.       format.html # index.html.erb   
  6.       format.xml  { render :xml  =>  @posts  }  
  7.     end   
  8. end   

实际上在这里有2个地方与rails2不同了,第一是查询的方式,以前我们可能会这样写

  1. @posts  = Post.find( :all )  

,那么现在这种方式完全改变了,现在多出了一个重要的类 ActivaRecord::Relation 在里面分装了大量的数据库相关的操作。第二呢,我们不用写成上面的这种代码了:

  1. respond_to  :html , :js , :xml   
  2. def  index  
  3.     @posts  = Post.all  
  4.     respond_with @posts   
  5. end   

这样很容易解释,相应的格式请求使用相应的模板,比如我们访问 http://localhost:3000/posts.xml 那么我们会得到一个xml格式的posts, 是不是很REST? 这里我们加了一个:js, 这为后面教程中使用ujs做下铺垫,那么具体的用法我们会在后面教程中给大家一一说明。

Rails把所有该动作中的实例变量传递给相应的视图,这里是 app/views/posts/index.html.erb :

  1. < h1 > Listing posts </ h1 >   
  2.    
  3. < table >   
  4.   < tr >   
  5.     < th > Name </ th >   
  6.     < th > Title </ th >   
  7.     < th > Content </ th >   
  8.     < th > </ th >   
  9.     < th > </ th >   
  10.     < th > </ th >   
  11.   </ tr >   
  12.    
  13. < % @posts.each do |post| % >   
  14.   < tr >   
  15.     < td > < %= post.name % > </ td >   
  16.     < td > < %= post.title % > </ td >   
  17.     < td > < %= post.content % > </ td >   
  18.     < td > < %= link_to 'Show', post % > </ td >   
  19.     < td > < %= link_to 'Edit', edit_post_path(post) % > </ td >   
  20.     < td > < %= link_to 'Destroy', post,  :confirm  = >  'Are you sure?',  :method  = >  :delete % > </ td >   
  21.   </ tr >   
  22. < % end % >   
  23. </ table >   
  24.    
  25. < br   />   
  26.    
  27. < %= link_to 'New post', new_post_path % >   

该视图遍历@posts数组来显示内容以及链接,一些要注意的地方:

  • link_to 绑定了特定的记录操作
  • edit_post_path 与 new_post_path 实际上在你设定 routes.rb 中相关resources的时候就已经给你生成的帮助器(好像一共有7个),你可以在控制器中看到不同的这种帮助器。

这里还有一点值得注意:在先前版本的rails中你可能需要使用 <%= h post.name %> 来实现HTML转义,那么在3.0中,默认就已经是转义的了,如果你需要获得未转义的HTML,你需要这样写 <%= raw post.name %> (raw我想大家很容易理解, java中也经常有这种警告)

2. 自定义布局

当rails渲染视图给浏览器时,它会把视图放置到布局内然后在输出。在先前的rails版本中,rails g scaffold 命令会自动创建控制器指定的布局,比如 app/views/layouts/posts.html.erb, 那么在3.0中 app/views/layouts/application.html.erb 作用与所有控制器。打开你的编辑器,把该布局修改如下:

  1. <!DOCTYPE html >   
  2. < html >   
  3. < head >   
  4.   < title > Blog </ title >   
  5.   < %= stylesheet_link_tag :all % >   
  6.   < %= javascript_include_tag :defaults % >   
  7.   < %= csrf_meta_tag % >   
  8. </ head >   
  9. < body   style = "background: #EEEEEE;" >   
  10.    
  11. < %= yield % >   
  12.    
  13. </ body >   
  14. </ html >   

现在你刷新你的页面你会发现背景变成了灰色。

3. 创建新的Post

创建一个新的Post将调用两个动作,第一个是new,它将初始化一个Post对象。

  1. def   new   
  2.     @post  = Post. new   
  3.     respond_with @post   
  4. end   

(同样我们在这里改为更简单的方式, 是不是很DRY?)

在 new.html.erb 视图中显示一个空的Post给用户:

  1. < h1 > New post </ h1 >   
  2.   
  3. < %= render 'form' % >   
  4.   
  5. < %= link_to 'Back', posts_path % >   

<%= render 'form' %> 是我们第一次介绍Rails的局部模板。局部模板实际上也是一个视图文件,只是该文件可以被多个视图重复引用。在这种情况下,form可用于创建,更新 post,然后我们可以看到 app/views/posts/_form.html.erb:

 

  1. < %= form_for(@post) do |f| % >   
  2.   < % if @post.errors.any? % >   
  3.     < div   id = "error_explanation" >   
  4.       < h2 > < %= pluralize(@post.errors.count, "error") % >  prohibited this post from being saved: </ h2 >   
  5.   
  6.       < ul >   
  7.       < % @post.errors.full_messages.each do |msg| % >   
  8.         < li > < %= msg % > </ li >   
  9.       < % end % >   
  10.       </ ul >   
  11.     </ div >   
  12.   < % end % >   
  13.   
  14.   < div   class = "field" >   
  15.     < %= f.label :name % > < br   />   
  16.     < %= f.text_field :name % >   
  17.   </ div >   
  18.   < div   class = "field" >   
  19.     < %= f.label :title % > < br   />   
  20.     < %= f.text_field :title % >   
  21.   </ div >   
  22.   < div   class = "field" >   
  23.     < %= f.label :content % > < br   />   
  24.     < %= f.text_area :content % >   
  25.   </ div >   
  26.   < div   class = "actions" >   
  27.     < %= f.submit % >   
  28.   </ div >   
  29. < % end % >   

局部模板可以同样接受视图的实例变量,当然也可以接受视图传递过来的变量。这里有一个另一种写法。

首先我们先稍微了解一下局部视图。

  • 局部视图都以 下划线('_')开头
  • 局部视图本事存在一个变量可以传递到局部视图里面,该变量是 去掉下划线,比如我的局部视图是 _form.erb, 那么在局部里面有存在一个 form 的变量, 当然还可以设定其他变量或者集合,这些我们将在后面的教程中详细学习。

现在,我们有一个 @post 变量, 那么我们可以把 render 改为 <%= render @post %>, rails很聪明,那么我们在局部视图中就会存在一个 post 变量,所以在局部视图中 把所有@post改为post,最后我们还要把 _form.html.erb 改为 _post.html.erb. 大家可以试一下,当然其实在本教程中完全没有必要,我这样说只是在为后面的教程做铺垫,这样大家就会更容易明白。

那么关于局部视图的用法我们会在后面的内容中详细的介绍。

我们现在再来看下 form_for, form_for 用于根据模型对象生成表单。比如 f.text_field :name 会告诉rails生成一个文本输入框,并且保存模型的name属性。(实际上生成的是 <input type='text' name='post[name]' />)

实际上 form_for 方法知道你是new还是edit,然后对提交按钮设置相应的value,(比如在这里new里面是create post, edit里面是update post)

如果你需要创建与模型无关的表单,可以使用 form_tag 方法

当用户点击 Create Post 按钮时,浏览器将会把表单信息提交给create方法(RAILS知道调用create方法,因为这是POST提交的):

  1. def create  
  2.     @post  = Post. new (params[:post])  
  3.     if   @post .save  
  4.       respond_with @post , :notice =>  'Post was successfully created.'   
  5.     else   
  6.       render :action => 'new'   
  7.     end  
  8. end  

create方法根据传递的post参数初始化了一个post对象,保存成功以后放回用户请求的相应格式。然后把用户重定向到show页面并且设置一条成功创建的信息反馈给用户。

如果post由于验证失败等原因未能成功保存,那么控制器将把用户返回到new页面,并显示相应的错误信息。

“Post was successfully created.”保存在rails的flash哈希内,我们可以在视图层用<%=flash[:notice]%>查看(那么现在我们只需要 =notice 就可以了)

 

来自:http://blog.csdn.net/emerald0106/article/details/7078906

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics