完整 Rails 5 条路线与祖先 Gem 嵌套树

Full Rails 5 routes with Ancestry Gem nested tree

我在我的 Rails 5 应用程序中使用 Ancestry Gem,所有的东西看起来都和它们应该的一样,我的数据库看起来也不错。我只是不知道如何在模型名称后的第一级之后显示完整的 url 路径。

例如

我需要 http://127.0.0.1:3000/pages/29 看起来像 http://127.0.0.1:3000/pages/22/29(我会在其工作后实现友好的 ID)。在上面的示例中,id:29 是祖先 id:22

的子页面

数据库截图

page.rb

class Page < ApplicationRecord
  has_ancestry
end

pages_controller.rb

...
private
...
def page_params
 params.require(:page).permit(:name, :content, :ancestry, :slug, :parent_id)
end
...

schema.rb

create_table "pages", force: :cascade do |t|
  t.string "name"
  t.text "content"
  t.string "ancestry"
  t.string "slug"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["ancestry"], name: "index_pages_on_ancestry"
end

routes.rb

...
resources :pages
root to: 'pages#index'
...

127.0.0.1:3000/rails/info/routes

Helper  HTTP Verb   Path    Controller#Action
Path / Url      
Path Match
stripe_event_path       /webhooks/stripe    
StripeEvent::Engine

new_user_session_path   GET /users/sign_in(.:format)    
devise/sessions#new

user_session_path   POST    /users/sign_in(.:format)    
devise/sessions#create

destroy_user_session_path   DELETE  /users/sign_out(.:format)   
devise/sessions#destroy

new_user_password_path  GET /users/password/new(.:format)   
devise/passwords#new

edit_user_password_path GET /users/password/edit(.:format)  
devise/passwords#edit

user_password_path  PATCH   /users/password(.:format)   
devise/passwords#update

PUT /users/password(.:format)   
devise/passwords#update

POST    /users/password(.:format)   
devise/passwords#create

cancel_user_registration_path   GET /users/cancel(.:format) 
devise/registrations#cancel

new_user_registration_path  GET /users/sign_up(.:format)    
devise/registrations#new

edit_user_registration_path GET /users/edit(.:format)   
devise/registrations#edit

user_registration_path  PATCH   /users(.:format)    
devise/registrations#update

PUT /users(.:format)    
devise/registrations#update

DELETE  /users(.:format)    
devise/registrations#destroy

POST    /users(.:format)    
devise/registrations#create

users_path  GET /users(.:format)    
users#index

POST    /users(.:format)    
users#create

new_user_path   GET /users/new(.:format)    
users#new

edit_user_path  GET /users/:id/edit(.:format)   
users#edit

user_path   GET /users/:id(.:format)    
users#show

PATCH   /users/:id(.:format)    
users#update

PUT /users/:id(.:format)    
users#update

DELETE  /users/:id(.:format)    
users#destroy

pages_path  GET /pages(.:format)    
pages#index

POST    /pages(.:format)    
pages#create

new_page_path   GET /pages/new(.:format)    
pages#new

edit_page_path  GET /pages/:id/edit(.:format)   
pages#edit

page_path   GET /pages/:id(.:format)    
pages#show

PATCH   /pages/:id(.:format)    
pages#update

PUT /pages/:id(.:format)    
pages#update

DELETE  /pages/:id(.:format)    
pages#destroy

root_path   GET /   
pages#index

Routes for StripeEvent::Engine
root_path   POST    /   
stripe_event/webhook#event

你的问题

您想要执行 GET 请求 /pages/:anchestry_id/:id 以执行操作 pages#show

解决方案

您需要在 routes.rb 文件的顶部添加一条特殊路线

get '/pages/:anchestry_id/:id' => 'pages#show', as: 'page'

您需要了解您希望在何处拥有该 link,因此您希望从应用中的哪些页面调用该操作 pages#show,因为您将需要转到该控制器操作并在该操作中检索 link 所需的 2 个变量,它们是 @anchestry@page

如果您是从 pages#index 执行此操作,那么您将有许多 link,您将不得不查询所有 @ancestries@pages 然后循环他们在视图中创建许多 links

pages_controller.rb

def index
    @ancestries = Ancestry.all
    @pages = Page.all
end

然后你需要在页面

中添加一个link
<%= link_to 'page', page_path(@ancestry, @page) %>

或者在 pages/index.html.erb 的情况下,将使用 @pages@ancestries do

<% @pages.each do |page| %>
     <%= link_to 'page', page_path(page.ancestry, page) %>
<% end %>

问题是,正如我从您的数据库中看到的那样,某些条目的祖先为 nil

31 About us Description of company [Null]

这将触发路线错误,因为您将经过 page.ancestry = nil 而您不能有路线 /pages/null/31

所以我认为您需要重新思考并考虑应用架构的设计。

另外,为什么要在您的路线中传递 ancestry 这一切都没有意义