找不到合适的控制器动作来渲染视图

时间:2014-10-02 20:12:24

标签: ruby-on-rails ruby controller routes

我正在尝试这样做:

县名单

县1(链接到县1内的位置 县2(链接到县2内的位置列表

县1内的地点列表

位置1(指向位置页面的链接 位置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

2 个答案:

答案 0 :(得分: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区分开来。这有意义吗?

当您使用named_route创建as:时,也无需创建生成路径的方法,这将生成使用as name plus _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

然后在您的视图中,您可以使用

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

这将在链接中生成/locations/location_name_here之类的路由,以便它们正常工作。

然而,您也可以将这些内容放在您的资源范围内,如下所示:

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

答案 1 :(得分:0)

你已经试过这样的事吗?

<%= link_to location.name, county_location_path(@county, location) %>
相关问题