繁体   English   中英

如何更改路线名称 rails 4

[英]How to change a route name rails 4

我更改了posts#index的路由以匹配blog ,现在我在我试图完成的URL 中得到/blog

我已经尝试了几种不同的东西,让我实际的博客文章该航线目前看起来像/posts/this-is-a-test还使用blog ,而不是posts的网址。
下面是我当前的路由文件。 我正在使用friendly_id gem,如果这对回答这个问题有任何影响的话。

resources :posts do
  resources :comments
end

  resources :contacts, only: [:new, :create]

  root "pages#home"

  get "/home", to: "pages#home", as: "home"
  get "about" => 'pages#about'
  get "pricing" => 'pages#pricing'
  get "contact_us" => 'pages#contact_us'
  match 'blog', to: 'posts#index', via: :all
end 

path选项和资源必须有帮助。

resources :posts, :path => 'blogs' do
  resources :comments
end

这会将所有/posts/post更改为/blogs//blog

如果你想改变你的路由的辅助方法,比如posts_pathblogs_pathnew_post_pathnew_blog_path等等,你可以用as标签来改变它。

resources :posts, :path => 'blogs', :as => 'blogs' do
  resources :comments 
end

或者更好的是,您可以直接将控制器和路由blogs指定为:

resources :blogs, controller: 'posts' do
  resources :comments
end

这就是 Rails 的厉害之处! :)

match 'blog/:id' => 'posts#show'

应该管用。 但是如果你想将 posts 控制器中的每个方法都匹配到 blog(并且你不想使用 posts 路径),我只会将控制器重命名为 blog 并在路由中添加资源 :blog 。

这帮助我从官方指南文档中将其添加到您的routes.rb文件中:

get '/patients/:id', to: 'patients#show', as: 'patient'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM