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 863the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8881. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9351. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 9121.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7891. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9271. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7341. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11441. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 783in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 812in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 696again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7371. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 764whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7861. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 766There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 687start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 669a 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 759in 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.rb`: 路由配置文件,定义URL与控制器动作的映射关系。 - `initializers`: 初始化脚本,加载各种配置。 - **db**:数据库相关文件夹,包含数据库迁移文件和初始化脚本。 - **test**:测试文件夹,包含...
Rails的路由系统(routes.rb)将URL映射到控制器的特定动作上,这允许开发者灵活地定义URL结构。通过资源路由,可以轻松实现RESTful操作。 **7. ActiveRecord查询接口** Rails 2.0提供了ActiveRecord查询接口...
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 ...
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
`config/routes.rb`文件定义了所有路由规则,包括资源路由、命名路由和自定义路由。 6. **视图模板**: 视图使用ERB(Embedded Ruby)或更现代的Haml、Slim等模板语言,结合HTML来渲染用户界面。图片的展示、上传...
- 配置文件`config/routes.rb`。 - 常见的路由类型: 默认路由、命名路由、约束路由等。 #### 七、练习作业1-建立Group-CRUD与RESTful - **CRUD操作**: - Create (创建): 创建新的Group对象。 - Read (读取): ...
4. **路由(Routes)**:定义了URL与控制器动作之间的映射,确保用户能正确访问购物车功能。 5. **会话(Session)**:Rails中的会话管理允许在多个请求之间保持状态,这对于购物车尤为重要,因为用户可能需要在...
4. **Routes**:Rails的路由系统负责将HTTP请求映射到相应的控制器动作。通过配置routes.rb文件,开发者可以定义资源、命名路由等,使URL管理更加灵活。 5. **Gemfile与Bundler**:Rails项目通常使用Gemfile来管理...
3. **config/**:配置文件夹,包含数据库配置、路由设置(routes.rb)、应用配置等。 4. **db/**:数据库相关文件,如数据库迁移(migrations)和数据库种子数据(seeds.rb)。 5. **app/**:应用的核心部分,包含...
RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, destroy)。有时我们需要自定义路由,这时就要用到:on参数。:on参数有三种取值,分别为collection,member,new。...
- **配置**:在`config/routes.rb`文件中添加新的路由规则,例如`get 'new_route' => 'controller#action'`。 - **效果**:这将在应用中增加一个新的URL路径,指向指定控制器的动作。 #### 七、渲染视图 - **方法*...