Rails中奇怪的语言环境问题

时间:2021-09-02 23:03:37

I have a url

我有一个网址

<li><%= link_to "#{t('feed.add')}", feed_path(locale) %></li>

And the problem is, that after the registration, when I press this link for the first time it redirects me to the mydomain.com/feed and prints the next error:

问题是,在注册后,当我第一次按下此链接时,它会将我重定向到mydomain.com/feed并打印下一个错误:

"feed" is not a valid locale

in the

def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
end

But when I return back and clicks again it redirects me to the:

但当我返回并再次点击时,它会将我重定向到:

mydomain.com/en/feed

So, what is the problem of this strange problem?

那么,这个奇怪问题的问题是什么?

UPDATE

devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
match '', :via => [:get], to: redirect("/#{I18n.locale}")
get '/en/youarethechampion' => "winners#winner"

get "/en/hook" => "feed#hook"
get '/thisvideonotfound' => 'feed#notFound'

get '/:locale/feed', to: 'feed#index', as: 'feed'
post '/:locale/feed/create', to: "feed#create"
get '/:locale/feed/new', to: "feed#new", as: 'feed_new'
get '/:locale/feed/destroy', to: "feed#destroy", as: 'feed_destroy'
get '/:locale/feed/edit', to: "feed#edit", as: 'feed_edit'
get '/:locale/feed/update', to: "feed#update", as: 'feed_update'

scope :path => "(:locale)", :locale => /en|ru/ do
  devise_for :users
end

match '/:locale/posts/search/', to: 'home#search', as: 'search', via: :get

get '/:locale/page/:page_slug' => 'home#about'
get '/:locale/:category/:id/', to: 'home#show', as: 'feed_show'
get '/:locale/:id/', except: 'search', to: 'home#by_category', as: 'category'
get '/:locale/posts/user/:name/', to: 'home#by_user', as: 'user'
get '/:locale/posts/tag/:tag/', to: 'home#by_tag', as: 'tag'
get '/:locale/pages/:page/', to: 'page#show', as: 'page'
post '/feed/:id/comments/', to: 'home#create_comment'
get '/:locale/', to: "home#index"

1 个解决方案

#1


0  

Ok, looks like scenario after the registration: locale is nil, and this line

好的,看起来像注册后的场景:locale是nil,这一行

<li><%= link_to "#{t('feed.add')}", feed_path(locale) %></li>

make link to just /feed. (Try it print it in front of the link)

链接到just / feed。 (尝试将其打印在链接前面)

<li>
  Locale is <%= locale %>
  <%= link_to "#{t('feed.add')}", feed_path(locale) %>
</li>

Then routes think that /feed is belongs to this route

然后路线认为/ feed属于这条路线

get '/:locale/', to: "home#index"

and it tries to set feed as the locale. It doesn't work! Then you move to another page you define "real" local through default_locale -> en.

并尝试将Feed设置为区域设置。它不起作用!然后通过default_locale - > en移动到另一个定义“真实”本地的页面。

I18n.locale = params[:locale] || I18n.default_locale

#1


0  

Ok, looks like scenario after the registration: locale is nil, and this line

好的,看起来像注册后的场景:locale是nil,这一行

<li><%= link_to "#{t('feed.add')}", feed_path(locale) %></li>

make link to just /feed. (Try it print it in front of the link)

链接到just / feed。 (尝试将其打印在链接前面)

<li>
  Locale is <%= locale %>
  <%= link_to "#{t('feed.add')}", feed_path(locale) %>
</li>

Then routes think that /feed is belongs to this route

然后路线认为/ feed属于这条路线

get '/:locale/', to: "home#index"

and it tries to set feed as the locale. It doesn't work! Then you move to another page you define "real" local through default_locale -> en.

并尝试将Feed设置为区域设置。它不起作用!然后通过default_locale - > en移动到另一个定义“真实”本地的页面。

I18n.locale = params[:locale] || I18n.default_locale