按照官网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. Have your GDX app run in the web browser

    https://code.google.com/p/libgdx-users/wiki/Applets—————————————————————————————————————————————— Ha ...

  2. javascript对下拉列表框(select)的操作

    <form id="f"> <select size="1" name="s"> <option value= ...

  3. 【BZOJ】3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(博弈论)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3404 写挫好几次.... 裸的博弈论即可.. #include <cstdio> #in ...

  4. 12 个免费在线的 Web 网站性能测试工具

    https://www.oschina.net/news/21033/12-free-online-tools-for-website-testing

  5. Ubuntu 16.04 LTS sublime text 3 解决不能输入中文

    sublime text 3 安装完成后不能输入中文,让人很是不爽.下面内容可以解决使用问题! 一.首先要注意几个问题. 1)sublime_imfix.c 文件放在home目录下面. 2)如果你在步 ...

  6. Hibernate中持久化类与持久化对象

    1.JavaBean类 JavaBean类是实体类,必须一下属性,private修饰的成员属性,public修饰的getter与setter访问方法,public修饰的空参构造器,实现Serializ ...

  7. SqlBulkCopy 通过泛型数组批量插入

    public void SqlBulkCopy<T>(string tablename, List<T> list) { Type recordType = typeof(T) ...

  8. VS 忽略文件 Git 向远程添加问题

    背景介绍: 我团队有个叫李恕华的技术大牛,最近在研究redis 然后向一个叫做MTS的项目中引入了redis,感觉很吊 问题: vs2017+git管理代码 其他人分支里一直提示找不到redis文件 ...

  9. Oracle Instant Client的安装和使用

    转自:https://www.cnblogs.com/chinalantian/archive/2011/09/09/2172145.html 根据自己需求到Oracle网站(http://www.o ...

  10. 纯css实现隐藏滚动条仍可以滚动

    移动端弹出层加了滚动条之后,滚动条一直在,有些不美观,overflow:hidden;虽然可以隐藏滚动条,但是不能滚动.需要实现隐藏滚动条但是仍可以滚动,js实现太麻烦,直接将滚动条隐藏会更好一些. ...