我的模特是:
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
Region_controller:
def show
@country = Country.find(params[:country_id])
@region = Region.find(params[:id])
end
主-NAV:
%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的名称“意大利”显示在主导航中时。我该怎么做?视图层中的条件?
在主导航中,变量@ region.name根据控制器逻辑显示正确的区域名称。但是当访问者在国家/地区页面时如何处理?主导航代码是@ region.name。
Thanks..remco
答案 0 :(得分:0)
# app/helpers/region_helper.rb
module RegionHelper
def region_name
return session[:region] if session[:region].present?
@region ||= Region.find('however you find')
@region.name
end
end
OR
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter :prepare_region
protected
def prepare_region
@region_name = session[:region] || Region.find('however you find')
session[:region] = @region_name
end
end
然后在您的视图中,如果修改第二个示例,请使用region_name
或@region_name
或@region.name
。使用您认为适合自己的任何代码的组合。