简体   繁体   中英

Rails Path Helper - same path for different namespaces

I'm building a rails app.
It has two namespaces that use same tables.
Is there any way to use same path helpers for different models under different namespaces that access one common table?

# namespaces (= roles)
admin
user
# tables
users
posts
# models
User < ApplicationRecord
Post < ApplicationRecord
Admin::User < User
Admin::Post < Post
User::Post < Post
# in controllers
## Admin::Post#index
@admin_posts = Admin::Post.all

## User::Post#index
@user_posts = User::Post.all
# in views
## Admin::Post#index
<%= render "shared/posts/index", posts: @admin_posts %>

## User::Post#index
<%= render "shared/posts/index", posts: @user_posts %>

I would like to use shared template.
<%= link_to post.title, post %> works. It'll generate admin/post/:id or user/post/:id depends on the namespace of the instance.
But is there any way to generate edit_post_path that adapts to its namespace?

edit_post_path seems can't do this, but you could try this:

<%= link_to post.title, [:edit, post] %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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