2014-10-02 27 views
0

我試圖做到這一點:在縣無法找到合適的控制器動作來渲染視圖

縣1人名單(鏈接的位置列表1 縣2(鏈接中縣1

位置1中列出郡的地點位置的2

名單(鏈接的位置頁 位置2等

我的路線如下:

Rails.application.routes.draw do 


resources :counties do 
    resources :locations 
end 


root 'home#index' 
get "/:county_name_with_prefix" => "counties#show_by_name" 
get "/:location_name_with_prefix" => "locations#show_by_name" 


end 

我CountiesController是這樣的:

class CountiesController < ApplicationController 
    before_filter :extract_county_name, :only => [:show_by_name] 
    **before_filter :extract_location_name, :only => [:show_by_name]** 

    def index 
     @counties = County.all 
     @locations = Location.all 

    end 

    def show 
     @county = County.find(params[:id]) 
     @locations = Location.all 

    end 

    def show_by_name 
     @county = County.find_by_name(@county_name) 
     @locations = Location.all 
     render :show 
    end 

    private 
    def extract_county_name 
    @county_name = params[:county_name_with_prefix].gsub(County::ROUTE_PREFIX,"").gsub("-", " ").strip 
    end 

    **private 
    def extract_location_name 
    @location_name = params[:location_name_with_prefix].gsub(Location::ROUTE_PREFIX,"").gsub("-", " ").strip 
    end** 


end 

縣指數的看法是這樣的:

<p>List of all counties</p> 

<ul> 
    <% @counties.each do |county| %> 
     <li><%= link_to "Locations in #{county.name}", generate_county_url_with_prefix(county) %></li> 
    <% end %> 
</ul> 

縣顯示查看是這樣的:

<h1>Domestic Cleaning Services in <%= @county.name %></h1> 

<ul> 
<% @locations.each do |location|%> 
<li><%= link_to "#{location.name}"**,generate_location_url_with_prefix(location) %></li>** 
<%end%> 

</ul> 

我可以得到這個工作,如果我刪除**stars**之間的代碼。然而,我想不出的是如何獲得鏈接到每個單獨位置頁面的位置列表 - 我的嘗試如**code marked like this**中所示。根據關係/數據,數據庫表格肯定可以設置好。任何想法大規模歡迎...

LocationsController:

class LocationsController < ApplicationController 
    before_filter :extract_location_name, :only => [:show_by_name] 

    def index 
     @location = Location.all 
    end 


    def show 
     @location = Location.find(params[:id]) 

    end 

    def show_by_name 
     @location = Location.find_by_name(@location_name) 
     render :show 
    end 

    private 
    def extract_location_name 
    @location_name = params[:location_name_with_prefix].gsub(Location::ROUTE_PREFIX,"").gsub("-", " ").strip 
    end  
end 

end 
+0

要覆蓋一個通過發送'/:county_name_with_prefix'和'/:location_name_with_prefix'這兩個路由將不起作用,因爲第二個條目將覆蓋首先,因爲你沒有顯示你路由到它的'LocationsController'將很難提供幫助。 – engineersmnky 2014-10-02 20:43:26

+0

@engineersmnky謝謝我已經添加了位置控制器 – user3735114 2014-10-02 21:00:34

+0

這很棒,但您是否注意到我關於重疊路線的其他評論?這個非常重要。 Rails無法區分指定的兩條路線。我會將它們更改爲「get」/ counties /:county_name_with_prefix「=>」counties#show_by_name「和'get」/ locations /:location_name_with_prefix「=>」locations#show_by_name「',這樣它們不會重疊,預期。 – engineersmnky 2014-10-03 15:13:10

回答

0

你已經嘗試過這樣的事情?

<%= link_to location.name, county_location_path(@county, location) %> 
+0

嗨,謝謝你。我在索引中使用@location,因爲我想要一個位於屬於他們belongs_to的縣下的位置列表。我唯一不能做的就是成功地從位置列表鏈接到位置頁面。所以County_1> List_locations_in_county_1沒問題。它剛從List_locations_in_county_1> Individual_location_page獲得了我的努力。知道我必須失去一些明顯的東西,但看起來很難! – user3735114 2014-10-02 20:50:09

+0

是的,第一次你可以嘗試分開你的路線...不要嵌套它們......在你的縣模型中寫has_many位置並寫入屬於縣的位置模型...然後嘗試在你的縣視圖中調用,而不是你的第一行@ county.locations.each做... – user934801 2014-10-02 22:12:52

+0

謝謝是的,我的模特已經成立了 - 沒有試圖打電話給@ county.locations.each做這是一個別人的建議。除了最後一個link_to從嵌套位置結果到位置頁面之外,它都可以正常工作。 – user3735114 2014-10-02 22:44:15

1

現在你有重疊的路線

get "/:county_name_with_prefix" => "counties#show_by_name" 
get "/:location_name_with_prefix" => "locations#show_by_name" 

這些都是含糊不清,因此第二將覆蓋第一引發他們都路由到locations#show_by_name

發生這種情況是因爲Rails無法區分/county_name_here/location_name_here。那有意義嗎?

此外,當您可以使用as:創建named_route時,不需要創建路由來生成路由,這將生成一個使用as名稱加上_path進行路由的方法。例如as: :hello將爲hello_path創建一個方法。

我建議像

get "/counties/:county_name_with_prefix", to: "counties#show_by_name", as: :county_by_name 
get "/locations/:location_name_with_prefix", to: "locations#show_by_name", as: :location_by_name 

然後在您的視圖中,可以使用像在鏈接/locations/location_name_here

<ul> 
    <% @locations.each do |location|%> 
     <li><%= link_to location.name,location_by_name_path(location_name_with_prefix: location.name) %></li> 
    <%end%> 
</ul> 

這將產生途徑,讓他們適當地工作。

您可以但是也可以範圍這些你的資源內,像這樣:

resources :counties do 
    get '/:county_name_with_prefix, to: 'counties#show_by_name', as: :by_name 
    resources :locations do 
    get '/:location_name_by_prefix', to: 'locations#show_by_name', as: :by_name 
    end 
end 

這將產生像

county_by_name   GET /counties/:county_name_with_prefix     counties#show_by_name 
county_location_by_name GET /counties/:id/locations/:location_name_with_prefix locations#show_by_name 

路線雖然這一切似乎是不必要的時候,你可以只使用產生像這樣的鏈接嵌套資源的默認路由。例如

<li><%= link_to location.name,county_location_path(location) %></li> 

這是因爲你的嵌套的資源已經創建了這條路線尋找像

county_location GET /counties/:county_id/locations/:id locations#show 

它將調用:county_id:idlocation和路線類似

/counties/1/locations/12 
相關問題