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
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 799the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8491. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9011. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8671.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7351. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8851. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6921. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10931. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 723in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 762in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 644again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6961. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 724whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7341. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 710There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 651start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 639a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 707in integration test, you can te ...
相关推荐
gem "rails-routes" 然后执行: $ bundle 或自己安装为: $ gem install rails-routes 用法 将这个gem添加到您的项目后,您可以在config/routes创建多个路由文件。 只要确保您用 # config/routes/v1.rb Rails . ...
《Learn Rails 5.2-2018》是一份重要的学习资源,专注于介绍2018年版本的Ruby on Rails框架。Ruby on Rails(RoR)是一个基于Ruby语言的开源Web开发框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化和...
将 Grape API 路由装入 Rails 后,Grape API 路由通常不会打印在rake routes或/rails/info/routes 。 这个 gem 将 Grape 的路由打印功能添加到 Rails 中。 用法 将此行添加到您的Gemfile gem 'grape-rails-routes...
Atom Rails Routes是一款专门为Ruby on Rails开发者设计的Atom编辑器插件。它提供了强大的自动化功能,以提高开发效率,特别是对于处理Rails应用中的路由工作。这款插件由两个主要组件组成:Autocomplete Provider...
ember-cli-rails-routes 安装 将此添加到您的Gemfile并bundle install gem 'ember-cli-rails-routes' 设置 Rails应用 在您的routes.rb文件中: ember_app :foo , scope : :app , path : 'frontend' 这反映了...
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 ...
`config/routes.rb`文件定义了所有路由规则,包括资源路由、命名路由和自定义路由。 6. **视图模板**: 视图使用ERB(Embedded Ruby)或更现代的Haml、Slim等模板语言,结合HTML来渲染用户界面。图片的展示、上传...
- 配置文件`config/routes.rb`。 - 常见的路由类型: 默认路由、命名路由、约束路由等。 #### 七、练习作业1-建立Group-CRUD与RESTful - **CRUD操作**: - Create (创建): 创建新的Group对象。 - Read (读取): ...
4. **Routes**:Rails的路由系统负责将HTTP请求映射到相应的控制器动作。通过配置routes.rb文件,开发者可以定义资源、命名路由等,使URL管理更加灵活。 5. **Gemfile与Bundler**:Rails项目通常使用Gemfile来管理...
RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, destroy)。有时我们需要自定义路由,这时就要用到:on参数。:on参数有三种取值,分别为collection,member,new。...
- **配置**:在`config/routes.rb`文件中添加新的路由规则,例如`get 'new_route' => 'controller#action'`。 - **效果**:这将在应用中增加一个新的URL路径,指向指定控制器的动作。 #### 七、渲染视图 - **方法*...
- **路由(Routes)**:Rails的路由系统将URL映射到特定的控制器动作,定义了应用的导航结构。在`config/routes.rb`文件中配置路由规则。 - **生成器(Generators)**:Rails提供了强大的生成器工具,可以自动生成...
《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...
- **Routes**:Rails的路由系统将URL映射到控制器的行动上,定义了应用程序的导航路径。 - **Scaffolding**:快速生成基本的CRUD(创建、读取、更新、删除)功能的工具,对初学者非常有用。 - **Gemfile**:定义...
- 引擎路由(Engine Routes)定义了引擎中各个组件的路由规则,使主应用能够通过路由访问引擎的功能。 - 引擎挂载(Engine Mounting)是指将引擎组件整合进主Rails应用中的过程,通过定义挂载点来实现。 3. 组件...
4. **路由(Routes)**:Rails的路由系统将URL请求映射到特定的控制器动作,实现了URL和应用逻辑之间的解耦。 5. **辅助方法(Helper Methods)**:为视图提供便利的功能,如链接生成、样式辅助等。 6. **Scaffold...