按照官网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. 微信小程序动态显示项目倒计时效果

    效果: wxml代码: <view class='spellNum'> <view> <text style='color: #fff;'>团长</text& ...

  2. ZoneDateTime 转换Date

    final ZonedDateTime now = ZonedDateTime.now(); //当前时间final ZonedDateTime todayZero = now.truncatedTo ...

  3. 尽管以C++为基础,但 Java 是一种更纯粹的面向对象程序设计语言

    “尽管以C++为基础,但 Java 是一种更纯粹的面向对象程序设计语言”. 无论C++还是Java 都属于杂合语言.但在 Java 中,设计者觉得这种杂合并不象在 C++里那么重要.杂合语言 允许采用 ...

  4. MFC多国语言——资源DLL

    此随笔中主要内容来自http://blog.csdn.net/china_hxx/article/details/10066655,原出处不详. 以下内容基于VC 6.0.要实现界面多语言化,必须要先 ...

  5. hdu 2074 叠筐 好有意思的绘图题

    叠筐 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  6. Segmented 标签栏 切换效果

    转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0512/1615.html http://www.it165.net/pr ...

  7. 2-SAT求任意解模板

    int stk[N],vis[N],low[N],link[N],mark[N]; int top,index,id,du[N];//记录入度数 int pre[N],cnt,g[N];// g 用来 ...

  8. rabbitmq 3.6.11 centos 7 安装

    http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm http://www.rabbitmq.co ...

  9. mysql5.7的密码

    [root@mysql ~]# grep "temporary password" /var/log/mysqld.log 2018-04-03T08:08:05.867624Z ...

  10. iOS 去掉navgationbar 底部线条

    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent]; [[UINavigationBar appearance] ...