2013-02-17 39 views
-1

我的車型有:路線得到正確的價值 - 基於URL

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 

主導航:

%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 /意大利名稱「意大利」顯示在主導航欄中。我該如何做到這一點?視圖層中的條件?

在主導航欄上的變量@ region.name顯示正確的區域名稱基於控制器的邏輯。但如何處理我當訪問者是該國網頁上?主要資產淨值代碼@ region.name。

謝謝..remco

+0

請重新說明這一點:「但現在我想也做國家道路......所以當一個訪客在/區域/意大利的名稱」意大利「顯示在主導航」我沒有得到你真的想要什麼 – mathieugagne 2013-02-17 21:09:29

+0

mathieugagne - 已更新的問題 – Remco 2013-02-17 21:19:37

+0

好的,我現在明白了,所以你有什麼嘗試?似乎你對Rails的瞭解有限。你是否熟悉'helpers'? – mathieugagne 2013-02-17 21:24:45

回答

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或​​。使用你覺得適合你的任何代碼的組合。

+0

mathieu.thanks..i do not如果我只有1個國家的記錄...這將在我的country_controller中工作 @region = Country.find(1)並在視圖中顯示'region'但是我的10個DB ... – Remco 2013-02-17 21:42:19

+0

那麼你需要展示什麼?你需要所有這些?'Country.all' – mathieugagne 2013-02-17 23:10:09