`
peryt
  • 浏览: 54914 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

5.2 rails routes

阅读更多

1. the purpose of rails routes:

 

a. recognize url, and dispatch to correct controller and actions

b. generate named routes, i.e. paths and urls, so the you needn't hardcode them in views

 

2. the simpleset route:

 

when rails receive a incoming request, 

 

GET /patients/17

 

it will ask the router to match a controller action, if the 1st match is:

 

match "/patients/:id", :to => "patients#show"

 

note, :to can be omited, so a simples way will be:

 

match "/patients/:id" => "patients#show"

 

then, the request is dispatched to patients controller, show action, with param hash {:id => "17"}


3. match "/patients/:id", :to => "patients#show"

this line of code will also create a named route (route helper):

 

so you can use it as:

 

@patient = Patient.find(17)

<%= link_to "parent 17", patient_path(@patient)%>

 the router will generate the path

/patients/17

note that you needn't metion the id in the route_helper (named route)

 

4. another important route is resourceful route for resourceful controller.

this route make you can create many good routes using just one line of code.

for example:

 

resources :photos

 

this will create many routes.

 

note that, if rails find the fisrt route that match the request, rails will not continue to search following routes.

 

5. using resourceful routes will also create some helpers (named routes) for use in views and controllers.

 

like:

photos_path,   ====> /photos

new_photo_path    ==============>/photos/new

edit_photo_path(:id)   =============> photos/17/edit

photo_path(:id)   =================> /photos/17

 

Since rails will also consider http verbs, so the four route helper map to 7 actions.

 

each of this has a corresponding _url helper.

_path is relative url

_url is full url

 

there are little difference between them.

 

6. you can define multiple resources at the same time.

 

 

resources :photo, :books, :videos
 

7. next, we talk about singular resource!!

 

for example, your client want to access profile without using id, since he is just using current login user.

 

so you can add this route:

 

match "/profile", "users#show"

 

the resource route is:

 

resource :geocoder

 

the auto-gen route helper are basically the same to plurable resources, but without id.

 

8. next is name space:

 

for example, you might want to group a group of controllers under admin, 

a. you need to put these controllers to app/controllers/admin/

 

b. in the routes file:

 

namespace :admin do

    resources :posts, :comments

end

 

 

 

 

 

分享到:
评论

相关推荐

    rails-routes:在Rails应用程序上启用configroutes * .rb

    gem "rails-routes" 然后执行: $ bundle 或自己安装为: $ gem install rails-routes 用法 将这个gem添加到您的项目后,您可以在config/routes创建多个路由文件。 只要确保您用 # config/routes/v1.rb Rails . ...

    Learn Rails 5.2-2018

    《Learn Rails 5.2-2018》是一份重要的学习资源,专注于介绍2018年版本的Ruby on Rails框架。Ruby on Rails(RoR)是一个基于Ruby语言的开源Web开发框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化和...

    grape-rails-routes:为 Grape with Rails 添加路由打印

    将 Grape API 路由装入 Rails 后,Grape API 路由通常不会打印在rake routes或/rails/info/routes 。 这个 gem 将 Grape 的路由打印功能添加到 Rails 中。 用法 将此行添加到您的Gemfile gem 'grape-rails-routes...

    atom-rails-routes:Rails路线的Autocomplete +和hy​​perclick提供程序

    Atom Rails Routes是一款专门为Ruby on Rails开发者设计的Atom编辑器插件。它提供了强大的自动化功能,以提高开发效率,特别是对于处理Rails应用中的路由工作。这款插件由两个主要组件组成:Autocomplete Provider...

    ember-cli-rails-routes

    ember-cli-rails-routes 安装 将此添加到您的Gemfile并bundle install gem 'ember-cli-rails-routes' 设置 Rails应用 在您的routes.rb文件中: ember_app :foo , scope : :app , path : 'frontend' 这反映了...

    routes源代码

    Routes is a Python re-implementation of the Rails routes system for mapping URLs to application actions, and conversely to generate URLs. Routes makes it easy to create pretty and concise URLs that ...

    Rails项目源代码

    `config/routes.rb`文件定义了所有路由规则,包括资源路由、命名路由和自定义路由。 6. **视图模板**: 视图使用ERB(Embedded Ruby)或更现代的Haml、Slim等模板语言,结合HTML来渲染用户界面。图片的展示、上传...

    Rails 101 入门电子书

    - 配置文件`config/routes.rb`。 - 常见的路由类型: 默认路由、命名路由、约束路由等。 #### 七、练习作业1-建立Group-CRUD与RESTful - **CRUD操作**: - Create (创建): 创建新的Group对象。 - Read (读取): ...

    rails指南 中文版

    4. **Routes**:Rails的路由系统负责将HTTP请求映射到相应的控制器动作。通过配置routes.rb文件,开发者可以定义资源、命名路由等,使URL管理更加灵活。 5. **Gemfile与Bundler**:Rails项目通常使用Gemfile来管理...

    Rails Routes中new、collection、member的区别浅析

    RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, destroy)。有时我们需要自定义路由,这时就要用到:on参数。:on参数有三种取值,分别为collection,member,new。...

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    - **配置**:在`config/routes.rb`文件中添加新的路由规则,例如`get 'new_route' =&gt; 'controller#action'`。 - **效果**:这将在应用中增加一个新的URL路径,指向指定控制器的动作。 #### 七、渲染视图 - **方法*...

    Ruby on Rails入门例子

    - **路由(Routes)**:Rails的路由系统将URL映射到特定的控制器动作,定义了应用的导航结构。在`config/routes.rb`文件中配置路由规则。 - **生成器(Generators)**:Rails提供了强大的生成器工具,可以自动生成...

    Rails101_by_rails4.0

    《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...

    rails 项目起步示例

    - **Routes**:Rails的路由系统将URL映射到控制器的行动上,定义了应用程序的导航路径。 - **Scaffolding**:快速生成基本的CRUD(创建、读取、更新、删除)功能的工具,对初学者非常有用。 - **Gemfile**:定义...

    component base rails applications

    - 引擎路由(Engine Routes)定义了引擎中各个组件的路由规则,使主应用能够通过路由访问引擎的功能。 - 引擎挂载(Engine Mounting)是指将引擎组件整合进主Rails应用中的过程,通过定义挂载点来实现。 3. 组件...

    Rails相关电子书汇总

    4. **路由(Routes)**:Rails的路由系统将URL请求映射到特定的控制器动作,实现了URL和应用逻辑之间的解耦。 5. **辅助方法(Helper Methods)**:为视图提供便利的功能,如链接生成、样式辅助等。 6. **Scaffold...

Global site tag (gtag.js) - Google Analytics