Search code examples
ruby-on-railsrubyhelper

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?


Solution

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

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