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 833the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8711. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9231. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8911.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7631. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9121. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7141. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11181. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 759in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 787in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 678again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7221. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 747whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7611. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 741There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 671start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 654a 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 737in integration test, you can te ...
相关推荐
轻松博客 ComfyBlog是的简单博客管理引擎 ...看一下config/routes.rb文件,您应该在其中看到以下几行: comfy_route :blog_admin , path : "admin" comfy_route :blog , path : "blog" 您还应该在/app/views/
在实践过程中,你还会遇到如ActiveStorage这样的功能,它是Rails 5.2引入的,用于处理文件上传,替代了原先的Paperclip和Carrierwave。还有ActionCable,它是Rails实现Websocket通信的组件,用于实现实时应用,如...
13. **ActiveStorage**: Rails 5.2 引入的存储解决方案,用于处理文件上传,支持直接存储到云服务如 AWS S3 或 Google Cloud Storage。 14. **Heroku 部署**: "rails-food-manager" 可能已经准备好部署到 Heroku ...
5. **存储机制**:Rails项目可能使用ActiveStorage,这是Rails 5.2引入的内置文件存储系统,可以方便地处理文件上传。ActiveStorage支持直接存储到本地文件系统或云服务如Amazon S3。 6. **安全性和权限**:为了...
- **路由配置**:使用 `config/routes.rb` 文件定义路由规则。 - **RESTful 路由**:支持 RESTful 设计风格的 URL 结构。 **5.3 ActiveRecord** - **模型定义**:定义数据库表对应的类。 - **数据库迁移**:用于...