【问题标题】:Full Rails 5 routes with Ancestry Gem nested tree带有祖先宝石嵌套树的完整 Rails 5 路线
【发布时间】:2017-10-30 03:56:53
【问题描述】:

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

【问题讨论】:

  • rake routes 说什么?
  • @sean 我用我的路线更新了我的问题。

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


【解决方案1】:

你的问题

您想执行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 这一切都没有意义

【讨论】:

  • 很好的答案。至于将祖先传递到我的路线中,我需要有 SEO 强大的 URL 模式。如果我离开祖先页面或父页面,那么 URL 的重要部分将丢失。这是有道理的还是我错过了什么?
  • @StudioRooster 抱歉,我不是 SEO 专家。我不知道你对ancestry 的意思,但是如果AncestryPage 模型之间存在关系,那么使用rails 的方法是guides.rubyonrails.org/routing.html#nested-resources 使用嵌套资源
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2014-12-31
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多