我的模型是:
class Country < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_many :regions
end
class Region < ActiveRecord::Base
belongs_to :country
end
部分路线
resources :countries, :path => ''
resources :countries, :path => '', :only => [] do
resources :regions, :path => ''
resources :regions, :path => '', :only => [] do
resources :houses do
collection do
get 'tags/:tag', to: 'houses#index', as: :tag
end
end
区域控制器:
def show
@country = Country.find(params[:country_id])
@region = Region.find(params[:id])
end
主导航:
%ul.nav
%li
= link_to "home", root_path
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown", :href => "/nl"}
= t('navigation.nav.houses')
= @region.name
主导航的区域名称(@region.name)由控制器变量填充。这很好用..所以我每个地区都得到了很好的“自定义”主导航。但是现在我也想按国家/地区路径进行操作...所以当访问者在 /locale/italy 上时,主导航中会显示名称“italy”。我该怎么做?视图层中的条件?
在主导航中,变量@region.name 根据控制器逻辑显示正确的区域名称。但是当访问者在国家页面上时我怎么办?主要导航代码是@region.name。
谢谢..remco