-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Namespaced models not working #1291
Comments
@TrevorHinesley can you provide an example app where you get that error? The example app included in this repo has namespaced models (blog/post.rb and dashboards/blog/post_dashboard.rb and it seems to be working fine. |
If you were to bundle PaperTrail into that example app and include it in a model, you'd get the same problem. Maybe it's PaperTrail specific, but it seems tied to namespacing somehow, because PaperTrail isn't doing anything special under the hood. Perhaps it's namespaced associations that are the problem? |
I'm running into the same issue at the moment. I've created an example app that only includes paper_trail and administrate here: https://github.com/coffeejunk/administrate-paper_trail Notes:
|
@coffeejunk Can you try following routes. You can create new folder
|
@usman-ahmad interesting.. after moving the and upon a refresh I don't get the error anymore: |
@coffeejunk This is what I am experiencing as well. However on production (Heroku) the page is not working no matter how many times I might refresh. Seems like a loading issue. I am not sure if this is a gem-specific issue. EDIT: Following the blog/posts way as in the example application made it work fine in production. In development I get the refresh issue, but I don't really mind as long as it works fine in production.
|
I had the same issue as in the OP and could fix it in my case with same code as suggested (and didn't encounter any refresh issues so far): # config/routes.rb
namespace :admin do
...
namespace :paper_trail do
resources :versions
end
end
# app/controllers/admin/paper_trail/versions_controller.rb
module Admin
class PaperTrail::VersionsController < Admin::ApplicationController
# To customize the behavior of this controller,
# you can overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = PaperTrail::Version.
# page(params[:page]).
# per(10)
# end
# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# PaperTrail::Version.find_by!(slug: param)
# end
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information
end
end
# app/dashboards/paper_trail/version_dashboard.rb
require "administrate/base_dashboard"
class PaperTrail::VersionDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
item: Field::Polymorphic,
id: Field::Number,
event: Field::String,
whodunnit: Field::String,
object: Field::Text,
created_at: Field::DateTime,
}.freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [
:item,
:id,
:event,
:whodunnit,
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = [
:item,
:id,
:event,
:whodunnit,
:object,
:created_at,
].freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [
:item,
:event,
:whodunnit,
:object,
].freeze
# Overwrite this method to customize how versions are displayed
# across all pages of the admin dashboard.
#
# def display_resource(version)
# "PaperTrail::Version ##{version.id}"
# end
end Should Administrate generate this automatically? |
Looks like I reached a workable state in a project combining Globalize v5.3.0, PaperTrail v10.3.1 (which both use namespaced models) with Administrate v0.12.0, using the following steps:
Some other configurations were then also needed (e.g for models using ActiveStorage attachments, or models with custom |
In case anyone else is experiencing issues with getting the namespaced models to work both on development and in production, here's what works for me on Rails 5.2.2 Your controller should look like this:
If you are using administrate in namespaced mode (eg. if it's accessible via **NOTE**: If you define your controller as below, it won't work on production, at least not on rails 5.2.2
Your dashboard should look like this:
Your routes should look like this
|
Adding a bit more of context. The main issue is that Administrate assumes the names of models following Rails conventions only. There isn't an official way of telling a dashboard that it should be serving a model different from the one named in the current URL. Due to this, if you want to provide a dashboard for a model, you must ensure that all necessary pieces are in place, and have the expected names. For example, for The admin controller will be under the ### app/controllers/admin/paper_trail/versions_controller.rb
module Admin
module PaperTrail
class VersionsController < Admin::ApplicationController
end
end
end The route will reflect this namespacing too: ### config/routes.rb
Rails.application.routes.draw do
namespace :admin do
resources :products
namespace :paper_trail do
resources :versions
end
end
end The dashboard will also need the ### app/dashboards/paper_trail/version_dashboard.rb
require "administrate/base_dashboard"
module PaperTrail
class VersionDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
event: Field::String,
object: Field::Text,
}.freeze
COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys
SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys
FORM_ATTRIBUTES = ATTRIBUTE_TYPES.keys
end
end With any other dashboards referring to your model via associations, use the option ### app/dashboards/product_dashboard.rb
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
code: Field::String,
price_in_cents: Field::Number,
stock: Field::Number,
versions: Field::HasMany.with_options(class_name: 'PaperTrail::Version'),
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze And that should be it. The situation is not ideal, and it stems from the way that Or, someone could try implement the solution at #405, and at least we'll have real code to discuss. Doesn't mean it will definitely be merged! But at least it helps understanding the problem better. |
… to see all messages sent by buyers to dog sellers Note: When generating/creating an `administrate` dashboard that is namespaced e.g. Buyer::MessageDashboard, administrate does not seem to put them automatically in the correct folder/namespace hierarchy to properly autoload the files … so you need to manually create the proper folder structure as shown in this open thoughtbot/administrate issue: thoughtbot/administrate#1291 (comment)
In Rails 6.0.3.2 the above solution does not appear to quite work I'm getting: NoMethodError (undefined method `admin_tenants_path' for #Admin::Manyhands::TenantsController:0x00007f959144c1a8): This is in the _form.html.erb file, which is receiving:
The problem is here:
For the 'new' routes, it'll use the key_strategy lambda, which uses the ActiveModel::Name.param_key value ('tenant') in this case. For routes where the mode is persisted, it'd use singular_route_key (also 'tenant') Near as I can see, I have 2 options:
becomes
NOTE If you're coming behind me w/ the same problem, note that the solution above is not complete, because you have to discern between the plural (POST) and singular (PUT/PATCH etc) paths. If this is the direction I go, I guess I'll create some kind of helper to generate this. Is there a better way to do this that I'm not seeing? |
When setting up for namespace :admin do
scope module: :active_storage do
resources :attachments
resources :blobs
end
end |
@andrewdsmith - when I change from:
to
I wind up with an error on /admin/tenants, where it tells me: This is called from: app/views/administrate/application/index.html.erb:44
|
@jeffdeville My solution probably only works because I'm not allowing the creation of new attachments or blobs, so I don't encounter the code that's causing an error in your case. I was really just commenting above to add an option for people to try who end up at this issue while trying to problem solve. I'm just baffled by why I had to switch to |
@jeffdeville - Thank you for your description of the issue. If you run the following on the console, do you get the same I get? > model = Manyhands::Tenant.first
Manyhands::Tenant Load (0.8ms) SELECT "tenants".* FROM "tenants" ORDER BY "tenants"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Manyhands::Tenant id: 1, name: "Foo">
> model.model_name.singular_route_key
=> "manyhands_tenant"
> model.model_name.param_key
=> "manyhands_tenant" From what you say, it looks like instead you get I'm surprised as I don't think that's the default behaviour. Is it possible that there's something else in your models that makes the result be different? |
Related/possible fix for the use cases mentioned here as well: #1037 (comment) |
I researched this area recently. The reason why
So using Another workaround I have found is using
|
Regarding the problem with |
By the way, I think the "solution" for this issue is documenting it, probably in the Guides, which I should write more for... I'll leave this open until I get around to doing it. Also this comment will remind me of what I'm supposed to do. |
Per #871 and #179, it looks like namespaced models were worked on before, but I'm still getting the same issue. Going to the "New " page or trying to edit an object that has PaperTrail results in:
Which looks to be the same namespacing issue as before. I'm on version 0.11.0 of the gem, as well as Rails 5.2.1 and Ruby 2.5.1.
The text was updated successfully, but these errors were encountered: