`
peryt
  • 浏览: 54393 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • 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

 

 

 

 

 

分享到:
评论

相关推荐

    comfy-blog:ComfortableMexicanSofa的博客引擎(Rails 5.2+)

    轻松博客 ComfyBlog是的简单博客管理引擎 ...看一下config/routes.rb文件,您应该在其中看到以下几行: comfy_route :blog_admin , path : "admin" comfy_route :blog , path : "blog" 您还应该在/app/views/

    syakyo_rails_app:学习开发rails,仿书

    在实践过程中,你还会遇到如ActiveStorage这样的功能,它是Rails 5.2引入的,用于处理文件上传,替代了原先的Paperclip和Carrierwave。还有ActionCable,它是Rails实现Websocket通信的组件,用于实现实时应用,如...

    rails-food-manager

    13. **ActiveStorage**: Rails 5.2 引入的存储解决方案,用于处理文件上传,支持直接存储到云服务如 AWS S3 或 Google Cloud Storage。 14. **Heroku 部署**: "rails-food-manager" 可能已经准备好部署到 Heroku ...

    rails-disk-project

    5. **存储机制**:Rails项目可能使用ActiveStorage,这是Rails 5.2引入的内置文件存储系统,可以方便地处理文件上传。ActiveStorage支持直接存储到本地文件系统或云服务如Amazon S3。 6. **安全性和权限**:为了...

    Ruby学习思维导图.pdf

    - **路由配置**:使用 `config/routes.rb` 文件定义路由规则。 - **RESTful 路由**:支持 RESTful 设计风格的 URL 结构。 **5.3 ActiveRecord** - **模型定义**:定义数据库表对应的类。 - **数据库迁移**:用于...

Global site tag (gtag.js) - Google Analytics