2

I recently added localizations scope to my routes (like described in Railscasts PRO #138), because I need them to look like this:

localhost/en/admin/posts
localhost/en/admin/posts/happy-new-post/edit

But adding a scope throws an error: can't find record with friendly id: "posts" and here is what my params are looking in this case:

Request

Parameters:

{"page_category_id"=>"admin", "id"=>"posts"}

here is what my config/routes.rb looks like:

Rails.application.routes.draw do



  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
    namespace :admin do
        get '', to: 'dashboard#index', as: '/'

      namespace :settings do
        get '', to: 'dashboard#index', as: '/'
        resources :general_settings, except: :show
      end
        resources :pages, except: :show
      resources :posts, except: :show
      resources :page_categories, except: :show
      resources :post_categories, except: :show
      resources :users, except: :show
    end
  end
end

Here is my Post-model if it helps:

class Post < ApplicationRecord

  include Bootsy::Container
  belongs_to :post_category

  validates :title, presence: true

  extend FriendlyId
  friendly_id :title, use: [:finders, :slugged]

  translates :title, :body
  globalize_accessors :locales => [:en, :ru], :attributes => [:title, :body]
end

Is there a way to make a url like localhost/en/admin/posts/happy-new-post/edit work?

mohnstrudel
  • 639
  • 8
  • 22
  • Does it help if you change it to `scope path: ':locale', ...`? The syntax is normally also: `scope ":locale", constraints: { locale: /#{I18n.available_locales.join("|")}/ } do`. I also find it helpful to look at the output of `rake routes` – Marc Rohloff Feb 24 '17 at 19:10
  • @mohnstrudel your code looks good. What is a request URL for those parameters `{"page_category_id"=>"admin", "id"=>"posts"}`? – bgs Feb 24 '17 at 22:33
  • @MarcRohloff Marc, unfortunately it does not help. My `rails routes` output is a bit crazy, I better screenshot it, rather than post in the comment - http://take.ms/dtlZm @bgs Bgs, simply `http://localhost:3000/admin/posts/` This is a complete screenshot, if it helps - http://take.ms/O7ROK Well, I actually managed to make a workaround by saving the selected locale in a session. I saw somewhere, that it is highly recommendable to make it clear to user, which locale is currently selected. For now, it doesn't bother me too much for my admin part, but I'll have the issue again for front soon:) – mohnstrudel Feb 27 '17 at 10:00
  • Are you sure that there isn't something else in your routes file that conflicts? Normally the parameters would include the `controller` and `action` parameters. – Marc Rohloff Feb 27 '17 at 18:41
  • They do include those parameters if I omit the `scope path: ':locale'` part. Or if I remove the friendly id gem. Those are my routes on another project with globalize and the `scope` part: `root GET /:locale(.:format) front/static_pages#home {:locale=>/en|ru/} admin GET /:locale/admin(.:format) admin/dashboard#index {:locale=>/en|ru/} admin_employees GET /:locale/admin/employees(.:format) admin/employees#index {:locale=>/en|ru/}` – mohnstrudel Feb 28 '17 at 18:08

0 Answers0