2016-10-18 13 views
3

名前空間について学びたい。Rails - 名前空間コントローラを使ってファイルを整理する

私は以前このトピックに関するいくつかの質問をしましたが、何が起こっているのか分かりません。

私は、コントローラのフォルダに 'features'という名前のフォルダを作成しました。その中でapp_roles_controller.rbというファイルを保存しました。

そのコントローラの最初の行は次のとおりです。

class Features::AppRolesController < ApplicationController 

機能フォルダの目的は、私は(それはそれだ)、より良い自分のファイルを整理することができます。私のroutes.rbをで

は、私が試してみました:

resources :app_roles, :namespace => "features", :controller => "app_roles" 

私も試してみました:

namespace :features do 
    resources :app_roles 
end 

私はモデル(トップレベル)と呼ばれるapp_role.rbを持っていると私は意見を持っていますフォルダは、ビュー/機能/ app_rolesとして保存され、インデックス、showなどのファイルが格納されています。 。私のスキーマ内の表が「app_roles」と呼ばれている

私はapp_rolesのルートをすくいとき、私は得る:

Paths Containing (app_role): 
app_roles_path GET /app_roles(.:format)  
app_roles#index {:namespace=>"features"} 

POST /app_roles(.:format)  
app_roles#create {:namespace=>"features"} 

new_app_role_path GET /app_roles/new(.:format)  
app_roles#new {:namespace=>"features"} 

edit_app_role_path GET /app_roles/:id/edit(.:format) 
app_roles#edit {:namespace=>"features"} 

app_role_path GET /app_roles/:id(.:format)  
app_roles#show {:namespace=>"features"} 

PATCH /app_roles/:id(.:format)  
app_roles#update {:namespace=>"features"} 

PUT /app_roles/:id(.:format)  
app_roles#update {:namespace=>"features"} 

DELETE /app_roles/:id(.:format)  
app_roles#destroy {:namespace=>"features"} 

が、私はそれが私が間違ってやっているということであるかを理解することはできません

私が試してみてください。

uninitialized constant AppRolesController 
を:

http://localhost:3000/app_roles#index 

私が言うエラーが出ます

は私がしようとすると:私はこれを設定する方法の平易な英語の説明を探しています

No route matches [GET] "/features/app_roles" 

http://localhost:3000/features/app_roles#index 

は、私が言うエラーが出ます。私はプログラミングルビーの本を試しました(数回以上)。

私はレールアプリで編成ファイルを導入するために何が起こる必要があるのか​​理解してもらえますか?

+0

は真剣に、あなたはこのURLをトリガされます。

だからあなたapp/controllers/features/app_roles_controller.rbファイルには、次のようになりますか? ' http:// localhost:3000/features/app_roles#index'?あなたのURLの '#index'を削除してください。 – araratan

+0

@araratan - どちらもうまくいきません。私が試してみると、http:// localhost:3000/features/app_rolesと言うと、次のようなエラーが表示されます:[GET] "/ features/app_rolesと一致しないルート" – Mel

+1

ルーティングに関するレールガイドを読んだことはありますか?それはしばしば見るのに良い場所です。名前空間に関するセクションは、以下の通りです:http://guides.rubyonrails.org/routing.html#controller-namespaces- and routeこれは、 'resources:app_roles、module: 'features''のようなものを試してみることを示唆していますインデックスページの 'features_app_roles_path'のようなルートに移動します(しかし、そのガイドを読んで、それがどのように動作し、何を期待するかについてもっと理解することをお勧めします)。 –

答えて

0

他の試みが実際に正しいと思われます。名前空間の下にリソースapp_rolesのルートを生成する場合As outlined in the Rails documentationは、featuresあなたroutes.rbファイルに以下を追加することができます。

namespace :features do 
    resources :app_roles 
end 

を今、あなたはrake routesを実行する次のような表示されます。

   Prefix Verb URI Pattern       Controller#Action 
    features_app_roles GET /features/app_roles(.:format)   features/app_roles#index 
         POST /features/app_roles(.:format)   features/app_roles#create 
new_features_app_role GET /features/app_roles/new(.:format)  features/app_roles#new 
edit_features_app_role GET /features/app_roles/:id/edit(.:format) features/app_roles#edit 
    features_app_role GET /features/app_roles/:id(.:format)  features/app_roles#show 
         PATCH /features/app_roles/:id(.:format)  features/app_roles#update 
         PUT /features/app_roles/:id(.:format)  features/app_roles#update 
         DELETE /features/app_roles/:id(.:format)  features/app_roles#destroy 

たとえば、最初の行は、/features/app_rolesにGETリクエストを行うと、features/app_rolesコントローラのindexアクションにルーティングされることを意味します。あなたがhttp://localhost:3000/features/app_rolesを訪問する場合は言い換えれば

は、それがクラスFeatures::AppRolesControllerを持っていることを期待app/controllers/features/app_roles_controller.rbに位置していますindexアクションへのルート要求をでしょう。

class Features::AppRolesController < ApplicationController 
    def index 
    end 
end 
+0

それは、私も "機能"と呼ばれるモデルフォルダを作成し、そのフォルダにapp_role.rbを保つことを意味ですか?そうであれば、スキーマ内のテーブルの名前が「機能」への参照を含むものではなく、「app_roles」であることを伝えるために何かする必要がありますか。 – Mel

関連する問題