完整的Rails 5路由Ancestry Gem嵌套树

时间:2017-10-30 03:56:54

标签: ruby-on-rails ruby ruby-on-rails-5 ancestry

我在我的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

的子页面

数据库截图

enter image description here

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

1 个答案:

答案 0 :(得分:2)

您的问题

您想要执行GET请求/pages/:anchestry_id/:id以便采取行动pages#show

解决方案

您需要在routes.rb文件

的顶部添加特殊路线
get '/pages/:anchestry_id/:id' => 'pages#show', as: 'page'

您需要了解您希望拥有该链接的位置,因此您要从应用中的哪些页面调用操作pages#show,因为您需要转到该控制器操作并在该操作中检索该链接需要2个变量,即@anchestry@page

如果您是从pages#index执行此操作,那么您将拥有许多链接,您必须查询所有@ancestries@pages,然后在视图中循环它们以创建许多链接

pages_controller.rb

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

然后你需要在页面中添加一个链接

<%= link_to 'page', page_path(@ancestry, @page) %>

或者pages/index.html.erb,使用@pages@ancestries

<% @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 这一切都没有意义