-1

我的模型是:

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

4

1 回答 1

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

或者

# 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_nameor@region_name或 or 。@region.name使用您认为适合您的任何代码的组合。

于 2013-02-17T21:32:48.073 回答