gpt4 book ai didi

ruby-on-rails - Full Rails 5 路线与 Ancestry Gem 嵌套树

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:34 24 4
gpt4 key购买 nike

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

最佳答案

你的问题

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

解决方案

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

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

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

如果您从 pages#index 执行此操作,那么您将有很多链接,您将不得不查询所有 @ancestries@pages 然后在 View 中循环它们以创建许多链接

页面 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

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

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

关于ruby-on-rails - Full Rails 5 路线与 Ancestry Gem 嵌套树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47007949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com