按照官网http://guides.rubyonrails.org/getting_started.html上学习了一下例子。在过程中有很多刚开始没理解的地方,写下来。

首先,建立了一个resources :articles。然后rake routes出现了如下

$ rake routes
      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
new_article GET    /articles/new(.:format)       articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
        root GET    /                            welcome#index
 
这是已经建立的所有路由,四种url和不同的verb一起对应了七个方法。
这里要注意一下rails是按照顺序来匹配url的,比如说后面的链接

<%= link_to 'Back', articles_path %>,都是返回到index页面。但是articles_path对应的是/articles,对应了两个页面index和create,
为什么不是返回到create页面呢。因为index页面排序在前面。后面的article_path返回show页面也是同理。
 
测试方法:写一个create.html.erb文件,然后让模型中create方法不指向其他页面(例如注释掉create中所有内容),接着在config/routes.rb中resources :articles之前加上get 'articles' => 'articles#create'。那么就会优先转到create页面了。
 
 
关于new里的表单<%= form_for :article do |f| %>,这句话的意思就是为:article建立了一个表单,这个symbol的命名和这个应用没有关系,之前还以为因为resources :articles的缘故,其实它随便换成什么都可以。后面可以用这个标识来从这个表单里面取值。
 
关于redirect_to @article这种指向一个实例的用法和 <%= link_to 'Edit', edit_article_path(@article) %>这种实例的路径的用法,我觉得是应为:id这个属性是唯一的来标识一个实例,所以实例和:id可以用来替换的。当使用实例时,有时候根据需要可以默认自动转化为:id。所以redirect_to @article会指向controller/:id。
edit_article_path(@article)对应着edit_article_path(:id),指向controller/:id/edit
 
具体来说:redirect_to @article
     redirect_to  article_path(@article)
     redirect_to  article_path(@article.id)
            redirect_to  /articles/#{@article.id}
    都是等价的
    
 
 

Getting Started with Rails (1)的更多相关文章

  1. Rails sanitize

    The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. T ...

  2. nginx中error_page没有生效(nginx+passenger+rails)

    应用部署方式为 nginx + passenger + rails 当我想要用nginx来默认处理400以上状态时,发现在rails返回respose之后,nginx不会再次执行error_page( ...

  3. Ruby on Rails 创建https应用

    1. 创建证书请求文件条件:私钥+证书签名请求+opensslyum install -y opensslmkdir /root/ssl/ && cd /root/ssl/openss ...

  4. Rails 5 开发进阶

    Rails 5 开发进阶:https://www.gitbook.com/book/kelby/rails-beginner-s-guide/details   cancan : http://blo ...

  5. rails程序文件名命名规范

    1 一般文件名是用小写单词加下划线分割,但类的名字用骆驼法.例如 sessions_controller.rb中定义SessionsController. 2 helpers内的文件为辅助类,定义了许 ...

  6. rails中的form_for

    1 form_for方法是ActionView::Helpers::FormHelper模块内的方法,所以可以在ActionView的实例中直接调用 2 from_for方法的原型为form_for( ...

  7. rails中的session

    学rails toturial的时候,第八章一直觉得有点没吃透,后来看了两篇rails关于session和cookies源码分析的文章,cookie原理与实现(rails篇) 和session原理与实 ...

  8. Ubuntu配置Ruby和Rails

    安装curl sudo apt-get install curl 安装RVM curl -L https://get.rvm.io | bash -s stable 通过RVM来安装Ruby rvm ...

  9. rails

    http://ruby-toolbox.com/ ~/.gemrc --- :backtrace: false :benchmark: false :bulk_threshold: 1000 :sou ...

  10. 安装 Ruby, Rails 运行环境 常见的错误

    安装部署ruby on rails 的环境时并不是想的那么顺利 这个是我遇到的问题及解决的方式 参考安装博客: (1) https://ruby-china.org/wiki/install_ruby ...

随机推荐

  1. 宝宝书 & 网站

    1. 网站 妈妈帮 宝宝树 2. 图书推荐 育儿百科 育儿经 中国儿童智力方程 聪明宝宝营养食谱1001例

  2. Ubuntu中su认证失败

    Ubuntu安装后,root用户默认是被锁定了的,不允许登录,也不允许 su 到 root 解决方法 sudo -i,输入当前用户密码后以root权限登录shell,无时间限制.使用exit或logo ...

  3. Redis "MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk"问题

    今天在程序中,jedis put数据到redis过程中,“MISCONF Redis is configured to save RDB snapshots, but is currently not ...

  4. hdu3665-Seaside(SPFA,dijkstra,floyd)

    Seaside Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. 解决 UIView 设置背景为UIImage图片变型问题[XXX setBackgroundColor:[UIColor colorWithPatternImage:XXX]];

    [self.drawingViewsetBackgroundColor:[UIColorcolorWithPatternImage:[selfthumbnailWithImageWithoutScal ...

  6. IOS控件:分歧解决其(UILabel 和 IBAction)

    #import <UIKit/UIKit.h> @interface demo7_dayViewController : UIViewController { // 用来显示程序结果 IB ...

  7. ddos cc攻击简单介绍(转)

    何为syn flood攻击: SYN Flood是一种广为人知的DoS(拒绝服务攻击)是DDoS(分布式拒绝服务攻击)的方式之一,这是一种利用TCP协议缺陷,发送大量伪造的TCP连接请求,从而使得被攻 ...

  8. 《从零开始学Swift》学习笔记(Day 55)——使用try?和try!区别

    原创文章,欢迎转载.转载请注明:关东升的博客 在使用try进行错误处理的时候,经常会看到try后面跟有问号(?)或感叹号(!),他们有什么区别呢? 1.使用try? try?会将错误转换为可选值,当调 ...

  9. Leetcode-Construct Binary Tree from inorder and postorder travesal

    Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definiti ...

  10. Powershell Get-registerkey(susid)

    $servers=get-content D:\serverregister.txt Get-registerkey -ComputerName $servers | select computer, ...