activeadmin: Routing Errors When to_param is Overwritten
I overwrote to_param
in a model to implement friendly urls. Active Admin can no longer link to the admin urls that require ids without throwing a routing error.
party.rb:
# Should give me urls like this: /parties/23/name-of-party
def to_param
"#{id}/#{slug}"
end
def slug
self.name.parameterize
end
routes.rb:
resources :parties, :constraints => { :id => /[0-9]+\/.+/ }
I believe this a different problem than the other to_param
issues listed below as I’m getting a routing error, not a active record loading error. Specifically, I get a routing error when any active admin page attempts to build a link that requires an id.
Is there any way to force active admin to ignore the overwritten to_param method?
Other issue that involve to_param: #35: https://github.com/gregbell/active_admin/issues/35 #62: https://github.com/gregbell/active_admin/issues/62 #191: https://github.com/gregbell/active_admin/issues/191
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Comments: 35 (6 by maintainers)
This worked flawlessly for me. Thanks watson.
UPDATE: It works sporadically.
UPDATE 2: The following seems to be more stable.
OK, so it seems that the resources configurations for a parent resources are set by the options of the
belongs_to
. So in order to be able to use a custom finder for the parent resource, you need to do this:It doesn’t sound that different, but here’s a different solution: Run-time redefine the
to_param
method like this:@tomgrim this also worked for me, but I also needed to add an
after_filter
so that it reverted the change. Otherwise, the change remains active and it can affect the rest of the application (that is, links on the public site using ID for the param).Better than that, I preferred to use an
around_filter
, like this:@elsurudo I had the same problem and fixed it with the following code:
Source: http://stackoverflow.com/questions/7684644/activerecordreadonlyrecord-when-using-activeadmin-and-friendly-id
I encountered a similar problem to this, a code base I was working with had a slug on a
Foo
model (provided by friendly_id) that was defined as being scoped to another model:I won’t go into why this isn’t being treated as a nested resource in the active admin setup we are using, but because it isn’t we found that active admin ended up using the non-unique slug for its urls, i.e. a url for a ‘foo’ object might be http://localhost:3000/admin/foos/not-a-unique-slug/edit and this meant that which object actually got modified was unpredictable.
My solution was to override the inherited_resources resource path/url helpers in the controller so that the urls instead used the
id
of the model, i.e:This is a bit brittle as it relies on knowledge of the inner workings of both active_admin and inherited_resources, but it does fix the problem for us.
I hope this might help others looking at this issue, but I’m open to suggestions of a better way to handle this.
Until the patch above is merged, here is an improvement on the overriding approach which doesn’t require redefining the custom
to_param
semantics:In production, rails cache the classes. therefore, when you override the to_param method it’s affect the whole site.
The best solution I found is to use around_filter to override to_param again to the original state
I see this is an old thread but I wanted to comment on a solution that worked for me while hitting this same problem. I had redefined to_param on a Job model and what I did was make an AdminJob model that inherits from Job and defines to_param the standard way (return instance id). That is:
Then on Active Admin’s side I did:
I currently haven’t find any problems with this, it seems ActiveRecord is quite smart with inheritance.
Cheers
I’m closing this, while it’s a inherited_resources bug and not a ActiveAdmin one.
This should be the correct way to handle it:
This works perfectly for me 👍
Thanks @mdoyle13
I’m working on an app that makes use of @armstrjare’s solution with
class_eval
and we’re noticing some very strange behavior. Occasionally, visitors are reporting seeing links likeinstead of
I suspect this may be related to the fact that
class_eval
is temporarily redefining Page#to_param to use theid
instead of theslug
.The issue roots all the way down to inherited resources which offers a solution to this built in.
The best way to solve this is to add defaults for inherited resources in the controller block provided by active admin.
Where slug is the name of the column I am using to store the slug/permalink/etc. You can pass any method on the model into here to allow active admin to find the resource.
Hope this helps!