Ruby on Railsでアプリケーションを作っているのですが、/記事のコントローラ名/1111(記事id)が出るような状態となっています。
これをコントローラ名を取ってidのみにしたいのですが、できません。どなたか教えていただけないでしょうか?
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
やりたいことの目的は、リソースの浅いネストを表現したいということでしょうか?
resourcesの shallowメソッドというものがあります。
詳しくは、こちらのページのshallowメソッドを参照ください。
https://railsguides.jp/routing.html
違っていたらスミマセン。
投稿2018/05/26 13:52
総合スコア18
0
試してませんが
routes.rbで
lang
1 get '/:id', to: 'hogehoge#show' 2 # hogehogeControllerのshowメソッドに飛ぶ
でできませんか?
投稿2015/06/19 04:50
総合スコア1901
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答ありがとうございます。
こちらの方法でうまくいきませんでした。。。
2015/06/19 05:27
テスト環境で試してみましたが、正常にできました。
きちんとパラメータ:idも渡ってます。
routes.rbの他の設定とコンフリクトしてるのでは?
2015/06/19 05:40
rake routesではどのように出てますか?
テストした内容は以下の通り
コントローラ TestController
メソッド index
show
routes.rb
Test::Application.routes.draw do
resources :test
get '/:id', to: 'test#show'
end
rake routes の結果
test_index GET /test(.:format) test#index
POST /test(.:format) test#create
new_test GET /test/new(.:format) test#new
edit_test GET /test/:id/edit(.:format) test#edit
test GET /test/:id(.:format) test#show
PUT /test/:id(.:format) test#update
DELETE /test/:id(.:format) test#destroy
GET /:id(.:format) test#show
こんな感じです。
rake routesで確認してみては?
2015/06/19 07:05
rake routesの結果
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
routes.rbの中身
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }
devise_scope :user do
resources :articles
get '/:username', controller: 'users', action: 'show'
get '/users/sign_out', controller: 'devise/sessions', action: 'destroy'
get 'users/auth/:provider/disconnect', controller: 'omniauth_disconnects', action: 'update'
end
post 'user_invitation_beta_release/new'
get 'static/welcome'
root to: 'root#index'
resources :items
resources :contents
resources :articles do
resources :contents, only: [:create, :destroy, :change_order] do
resources :items, only: [:create, :destroy]
end
end
match "*path" => "application#handle_404", via: :all
end
といった感じです。
2015/06/19 14:07
device というgemを使っていませんか?
これが影響しているように思えます。
このモジュールは使ったことがないので何ともアドバイスできませんが・・・
ちなみに、先ほどの
get '/:id', to: "hedgehog#show"
は、routes.rbのどの辺に入れましたか?
また、localhost:3000/123等の期待しているURLにアクセスした際には、
RoutingErrorが出ましたか?
それとも予想外のページが表示されましたか?
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。