2016-04-03 6 views
4

Railsルートの詳細を理解しようとしています。Railsルート:入れ子、メンバー、コレクション、名前空間、スコープおよびカスタマイズ可能

メンバーとコレクション

# Example resource route with options: 
    resources :products do 
     member do 
     get 'short' 
     post 'toggle' 
     end 

     collection do 
     get 'sold' 
     end 
    end 

名前空間と範囲

# Example resource route within a namespace: 
    namespace :admin do 
     resources :products 
    end 

    scope :admin do 
     resources :products 
    end 

制約、

# Example resource route with options: 
get "/questions", to: redirect {|params, req| 
    begin 
     id = req.params[:category_id] 
     cat = Category.find(id) 
     "/abc/#{cat.slug}" 
    rescue 
     "/questions" 
    end 
} 
Redirect_to

カスタマイズ:

resources :profiles 

編集用resource profilesから元のURL。

http://localhost:3000/profiles/1/edit 

私はクリックするだけedit profileを通して利用可能なユーザーのためにそれを作り、以下のようにURLを見てみたいです。

http://localhost:3000/profile/edit 

また、ほとんどの大企業は、レールに彼らのルートを設計どのように高度なルーティングは、ありますか?新しいルートがあれば、本当にうれしいでしょう。

ありがとうございました!

答えて

1

はそれのために特異リソース使用:

resource :profile 

をコントローラに現在のユーザーのプロファイルを操作します。

複雑なルート(通常は名前空間)では、浅いルートを持つネストされたリソースとカスタムアクションがすべて必要です。

0

thisあなたは質問の最初の部分に答えることができます。

質問の第2部分に答えるには「プロファイル」は、あなたの特異リソースとして扱うことができます(名詞自体の特異性は単なるリソースを表します)。詳細な説明については、linkを参照してください。それが部材に作用するため

18
**Collection & Member routes** 
  • 部材ルートは、IDを必要とします。それはオブジェクトの 収集

:memberに作用するため

  • 回収経路はIDを必要としないパターン/:controller/:id/:your_method

    :collectionのパスがパターン/:controller/:your_method

    のパスを作成し、作成し

    For example :

    Railsのroutes
    map.resources :users, :collection => { :abc => :get } => /users/abc 
    map.resources :users, :member => { :abc => :get } => /users/1/abc 
    
    **Scopes & Namespaces routes** 
    

    namespaceとは、コントローラに 名、URIに、名前付きルートに影響を与えます。

    Prefix Verb   URI Pattern     Controller#Action 
        baz_posts GET /foo/posts(.:format)   bar/posts#index 
           POST /foo/posts(.:format)   bar/posts#create 
    new_baz_post GET /foo/posts/new(.:format)  bar/posts#new 
    edit_baz_post GET /foo/posts/:id/edit(.:format) bar/posts#edit 
        baz_post GET /foo/posts/:id(.:format)  bar/posts#show 
           PATCH /foo/posts/:id(.:format)  bar/posts#update 
           PUT /foo/posts/:id(.:format)  bar/posts#update 
           DELETE /foo/posts/:id(.:format)  bar/posts#destroy 
    

    scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do 
        resources :model_name 
    end 
    

    For Example :

    scope 'foo', module: 'bar', as: 'baz' do 
        resources :posts 
    end 
    

    のようにルートを生成します。

    スコープの方法は、あなたのきめ細かい制御を提供します

    名前空間メソッドは単純なケースです。すべての接頭辞です。

    namespace :foo do 
        resources :posts 
    end 
    

    としてルートを生成する:Railsのルートが順次実行される

    Prefix Verb  URI Pattern     Controller#Action 
        foo_posts GET /foo/posts(.:format)   foo/posts#index 
           POST /foo/posts(.:format)   foo/posts#create 
    new_foo_post GET /foo/posts/new(.:format)  foo/posts#new 
    edit_foo_post GET /foo/posts/:id/edit(.:format) foo/posts#edit 
        foo_post GET /foo/posts/:id(.:format)  foo/posts#show 
           PATCH /foo/posts/:id(.:format)  foo/posts#update 
           PUT /foo/posts/:id(.:format)  foo/posts#update 
           DELETE /foo/posts/:id(.:format)  foo/posts#destroy 
    
    **Constraints & Redirect** 
    

    、次のようにして条件 ログインを模倣することができる:

    match '/route' => 'controller#action', :constraints => Model.new 
    match '/route' => 'user#action' 
    

    最初ラインチェックw制約の条件が満たされている(すなわち、要求がモデルドメインから出ている場合)。制約が満たされている場合、要求はコントローラ#アクションにルーティングされます。

    We can add constraints to routes for multiple uses like for ip-matching, params matching, restrict format parameter, request-based restrictions etc as : 
    
    - ip-matching 
        => resources :model, constraints: { ip: /172\.124\.\d+\.\d+/ } 
    - filtering id params 
        => match 'model/:id', to: 'model#show' ,constraints: { id: /\d+/}, via: :get 
    - restrict format params 
        => match 'model/:id', to: 'model#show' ,constraints: { format: 'json' }, via: :get 
    - request-based constraints 
        => get 'admin/', to: 'admin#show', constraints: { subdomain: 'admin' } 
    
  • +0

    私は 'resource:questions'を持っていますが、' constraints:{subdomain: 'blog'} 'と' resource:posts'もあります。今、私がサブドメインのブログに行くとき、私はまだ質問を見ることができます。あなたは質問のためのアクセスを与えないようにブロックする – 7urkm3n