2

Rails 3 でのルーティングについて読んでいますが、必要なものを達成できませんでした。Rails 3 のルートはまだかなり新しいので、単に物事を見落としているか、過度に複雑にしている可能性があります。

これは私が達成しようとしているものです:

website/foofooコントローラへのルート、indexアクション

website/foo/indexfooコントローラへのルート、indexアクション

website/foo/barfooコントローラへのルート、barアクション

website/foo/randomfooコントローラへのルート、indexアクション

website/foo/bar/rondomfooコントローラへのルート、barアクション

ここで、「ランダム」は任意のテキスト、数字、パス (/new/x/w/y/23) などです。

と の両方を使用してみmatchましresourcescollectionが、基本ケースは処理されましたが、「ランダム」は処理されませんでした。

それぞれの名前付きパスも探しています。指定する必要がありますか、それとも生成されますか?

4

2 に答える 2

1

http://guides.rubyonrails.org/routing.htmlには、特にルート グロビングに関するセクションなど、非常に役立つ情報が山ほど含まれています。

上記で定義したものと正確に一致させるには、次のことができます。

# config/routes.rb
namespace :website do
  match 'foo'             => 'foo#index'
  match 'foo/index'       => 'foo#index'
  match 'foo/bar'         => 'foo#bar'
  match 'foo/*random'     => 'foo#index' # params[:random] will contain "hello/world" if the URL is /website/foo/hello/world
  match 'foo/bar/*random' => 'foo#bar'
end

オプションを使用して:as、名前付きルートを指定できます。

match 'foo' => 'foo#index', as: 'foo' # helper would be website_foo_path
于 2011-09-14T00:14:59.620 に答える
1

route globbingを探しています。

foo/bar/*additional => "foo#bar" 

例:

website/foo/bar/random # params[:additional] = "random"
website/foo/bar/random/2 # params[:additional] = "random/2"
website/foo/bar/random/and/more/1/random/stuff/ # params[:additional] = "random/and/more/1/random/stuff/"
于 2011-09-13T18:06:55.893 に答える