We have the locale in the default_url_options so our urls look like this:
/en/... or /de/...
this locale parameter isn't used for creating the menu entries in the header and hence rails can't find the route.
Is default_url_options
defined on ApplicationController
?
yes...I also defined it in BaseController
but it didn't help either.
I see conflicting documetation discussed on this SO thread, can you try the alternatives mentioned & see if it is just outdated documentation and & deprecated method that is your problem?
I've never used this before myself.
Thanks for the link, but overwriting ùrl_options
doesn't help.
It has to do s.th. with the new menu changes. Because the pathes for the links for 'view', 'edit' etc. in the index pages are generated properly using the parameters from default_url_options
.
Thanks, that's a great tip to start. I bet we're using the a
abre tag instead of link_to
in the menu, as I know we're using link_to
in the table actions.
Here's the snippet of code that builds the path to a menu item (stored as a proc, so executed on every request).
module ActiveAdmin
class Resource
module Routes
# Returns the routes prefix for this config
def route_prefix
namespace.module_name.try(:underscore)
end
def route_uncountable?
controller.resources_configuration[:self][:route_collection_name] ==
controller.resources_configuration[:self][:route_instance_name]
end
# Returns a symbol for the route to use to get to the
# collection of this resource
def route_collection_path(params = {})
route, required_params = [], []
route << route_prefix
if belongs_to? && !belongs_to_config.optional?
name = belongs_to_config.target.resource_name.singular
route << name
required_params << :"#{name}_id"
end
route << controller.resources_configuration[:self][:route_collection_name]
route << (route_uncountable? ? 'index_path' : 'path')
route_name = route.compact.join("_").to_sym
route_params = params.values_at(*required_params)
routes.send(route_name, *route_params)
end
private
def routes
Rails.application.routes.url_helpers
end
end
end
end
So, we're using named URL helpers to generate the menu links (from what I see). Could you try specifying a URL manually to your menu item and see what you have to do to get it to pick up default_url_options
?
You should end up with something like admin_posts_index_path
for the index collection, which might be provided via InheritedResources.
ActiveAdmin.register Post do
menu url: -> { admin_post_path(resource) }
end
I tried to use:
ActiveAdmin.register Post do
menu url: -> { admin_post_path(locale: :en) }
end
but it didn't work. I debuged into route_collection_path
and tried to get default_url_options
in different ways but I didn't succeed. It should be possible to get it by calling controller.default_url_options
but it only gives me an empty hash.
I'm having the same problem on rails 3.2.13, ruby 1.9.3 and the newest active admin from git.I have not been able to find a fix.
@joker-777 and @robotex82 please be sure you're using the absolute latest from GitHub. Just yesterday the related code was changed a lot from @pcreux's PR.
Given this configuration
ActiveAdmin.register Category do
menu url: ->{ admin_categories_path }
end
# and
class ApplicationController < ActionController::Base
def default_url_options
{f: 0}
end
end
I get a URL with the expected ?f=0
appended on the end.
But this doesn't change the fact that we definitely should include the default URL options in our menu links.
Okay, the real problem is we're doing this:
class Resource
module Routes
def resource_collection_path(params = {})
# ...
routes.send(route_name, *route_params)
end
def routes
Rails.application.routes.url_helpers
end
end
end
That completely ignores the default url options you have defined in your controller.
I thought it might be as simple as this:
def resource_collection_path(params = {})
controller.send(route_name, *route_params)
end
But unfortunately controller
is the controller class, not the current controller instance... @pcreux do you have any idea how to acces the controller instance?
You'd think we could do this:
class Resource
module Routes
class RouteBuilder
include Rails.application.routes.url_helpers
delegate :default_url_options, :to => :controller
But at the time this file is parsed, Rails.application
is nil...
I really need that working for one of my project.
Anyone has an idea how to workaround this one ?
I am not good enough yet to come up with something but might perform test of options with guidance.
Coming back to this thread, I should note that @joker-777's example works just fine, it just needs to be posts
instead of post
:
ActiveAdmin.register Post do
- menu url: -> { admin_post_path(locale: :en) }
+ menu url: -> { admin_posts_path(locale: :en) }
end
@Daxter Well thanks a ton, I got it working.
Lazy me I should have tested more, thanks for the hint.
Thanks @joker-777 also for digging this.
I simply added as advised by @Daxter
ActiveAdmin.register Post do
menu :priority => 5, url: ->{ admin_posts_path(locale: I18n.locale) }
end
Great !
Any hint on how to pass I18n.locale
to actions
links ?
I mean this actions :
index do
column :post
actions
end
@Phazz it should automatically be included if you've overridden default_url_options
class ApplicationController < ActionController::Base
def default_url_options
{locale: I18n.locale}
end
end
has this been solved? seems like there is a lot of solutions details on this thread...
@Daxter
To clarify, I meant changing the url of actions
when the locale in the url of the app is changed. In my app I do that with custom links that change the locale.
I have this but still the url of actions (view edit delete links) are formed with the default locale :
class ApplicationController < ActionController::Base
def default_url_options(options = {})
{locale: I18n.locale}
end
end
Thanks,
A workaround is to leverage cancan and create yourself the links including the "dynamic" locale i.e. :
actions defaults: false do |customer|
links = ''.html_safe
if can?(:read, Customer)
links += link_to I18n.t('active_admin.view'), admin_customer_path(customer, locale: I18n.locale), class: "member_link view_link"
end
if can?(:update, Customer)
links += link_to I18n.t('active_admin.edit'), edit_admin_customer_path(customer, locale: I18n.locale), class: "member_link edit_link"
end
if can?(:destroy, Customer)
links += link_to I18n.t('active_admin.delete'), admin_customer_path(customer, locale: I18n.locale), :method => :delete, :confirm => I18n.t('active_admin.delete_confirmation'), class: "member_link delete_link"
end
links
end
Same problem with filters.
Clicking a filter button will submit with the default locale and so switch the locale in a multi locale AA app.
Someone know how I could override this behaviour ?
Thanks
@Phazz this is the code that builds the filter submit and cancel buttons:
content_tag :div, :class => "buttons" do
f.submit(I18n.t('active_admin.filter')) +
link_to(I18n.t('active_admin.clear_filters'), "#", :class => "clear_filters_btn") +
hidden_field_tags_for(params, :except => [:q, :page])
end
If your locale is in the params hash, it should persist between requests.
@Daxter Thanks Sean for bearing with me on this.
I try to understand what's going on here in my app.
for example on a page :
http://localhost:3000/es/app/admin_users
I filter the index view and receive this url :
http://localhost:3000/en/app/admin_users?utf8=%E2%9C%93&commit=Filter&locale=es&order=id_desc
It switches from es to en.
in my ApplicationController
I have :
before_filter :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
flash[:alert] = "Locale is " + I18n.locale.to_s
flash[:alert] = "Locale is " + params[:locale].to_s
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
The flashes send me the correct locale before clicking on the filter.
my default locale is set in application.rb
:
config.before_configuration do
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
I18n.default_locale = :en
I18n.reload!
end
I am confused. Might it be my routing configuration ?
Hmm, let me try it out locally. That should work.
@Daxter any chances you looked into that ? I din't find out on my side.
Thanks,
@Daxter Sorry to bother you again but I am dry of solution and need that working for production start. Anyone has a hint on how to force all links/submit to use I18n.locale for sure?
Thanks in advance,
My apologies, I've just been really busy as of late. I'll take another look into this tonight, and hopefully come up with a solution once and for all.
On Jun 17, 2013, at 9:56 AM, Pierre Lempérière [email protected] wrote:
@Daxter Sorry to bother you again but I am dry of solution and need that working for production start. Anyone has a hint on how to force all links/submit to use I18n.locale for sure?
Thanks in advance,—
Reply to this email directly or view it on GitHub.
@Daxter Thanks Sean but I finally found out what I was doing wrong... My routes I changed them to :
scope ":locale", :path_prefix => '/:locale' do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
end
I appreciate your dedication and want to give back I will publish an article on I18n with AA on the wiki for your review in the next weeks. I must give back what I learned from you guys for helping newcomers to AA.
Glad you figured it out. And it'd be great to have a comprehensive guide for translating AA apps. :)
Published a page in the wiki https://github.com/gregbell/active_admin/wiki/Switching-locale
Please review it, comments, modify, especially my english if I am not clear.
Sound like this could be closed
Hi,
Hope I'm not missing something but I ran into this again despite
following the guides here.
Server is running and I'm on localhost:3000/de/admin
.
But all generated urls are missing the locale:
http://localhost:3000/superuser/users/78609/edit
instead ofhttp://localhost:3000/de/superuser/users/78609/edit
Is there a way to avoid having to add the locale to all _path(locale: '123123')
?
Sorry I didn't realise there was another guide. Which addresses the behaviour:
Anyway you will notice that all links keep the default locale of your app.
and offers the same solution I came up with:
You can override this default locale in links in you active admin pages by passing the locale.
I've edited the wiki page to make this a bit more visible.
Hi @timoschilling,
I don't want to annoy you guys, but do you mind re-opening this? I don't believe that passing the locale around is a great solution. (That seems really error-prone and a lot of work).
Or am I misunderstanding the problem?
(I've also since summarised my problem on stackoverflow which might be more comprehensive.)
Ok so I've finally figured out what was going on.
I originally followed the rails guide and set my routes.rb:
# config/routes.rb
scope "/:locale" do
resources :books
end
which resulted in an error like:
No route matches {:action=>"index", :controller=>"admin/users"} missing required keys: [:locale]
this was "fixed" by setting
scope ':locale', defaults: { locale: I18n.locale } do
ActiveAdmin.routes(self)
end
as suggested in the current version of the Switching locale guide.
But this has the side-effect of all subsequent url_helpers using this locale for url generation. BTW at least one other person ran into this.
My current work-around can be found here:
lib/active_admin/helpers/routes/url_helpers.rb
def self.default_url_options
(Rails.application.config.action_mailer.default_url_options.merge({locale: ::I18n.locale})) || {}
end
Now urls are generated as expected.
If someone has an idea how I could fix this without adding locale to the default_url_options everytime, I'd be happy to submit a PR.
I used @wpp monkey patch and works without manually overriding all links...
Now the default_url_options
method has changed so I adapted it as below
lib/active_admin/helpers/routes/url_helpers.rb
def self.default_url_options
options = Rails.application.routes.default_url_options || {}
options.merge({locale: ::I18n.locale}) if defined?(::I18n) && ::I18n.locale
end
Maybe I can write a PR if somebody still has this issue...
Hi @B3rs, glad it helped. I for one would love a PR. I'm not sure why this issue hasn't been re-opened.
@timoschilling what do you think? here the real issue seems to be that we are using the default_url_options
from the rails initializers instead of the application_controller one (which correctly adds locale).
Is there a way to correctly reference ApplicationController's method or can we just add locale support?
Chiming in here because just ran into this issue. The guides here asks you to provide locale
option every single time which is cumbersome and error prone.
The urls that were auto generated by active admin like for user emails and such have the locale correctly set on them but all the urls generated by *_path
methods seem to default to :en
Most helpful comment
I used @wpp monkey patch and works without manually overriding all links...
Now the
default_url_options
method has changed so I adapted it as belowMaybe I can write a PR if somebody still has this issue...