【问题标题】:Can't find suitable controller action to render view找不到合适的控制器操作来呈现视图
【发布时间】:2014-10-02 20:12:23
【问题描述】:

我正在尝试这样做:

县列表

县 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>

Counties Show 视图是这样的:

<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** 中。就关系/数据而言,数据库表肯定设置得很好。任何想法都受到广泛欢迎...

位置控制器:

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

【问题讨论】:

  • 您正在通过发送 /:county_name_with_prefix/:location_name_with_prefix 来覆盖路由,这将不起作用,因为第二个条目将覆盖第一个条目,因为您没有显示您要路由到的 LocationsController这将很难提供帮助。
  • @engineersmnky 感谢我添加了位置控制器
  • 这很好,但您是否注意到我关于重叠路线的其他评论?这个非常重要。 Rails 无法区分指定的 2 条路线。我会将它们更改为get "/counties/:county_name_with_prefix" =&gt; "counties#show_by_name"get "/locations/:location_name_with_prefix" =&gt; "locations#show_by_name",这样它们就不会重叠并且可以按预期工作。
  • @engineersmnky 是的,你完全正确。这也让我对路由有了更多的了解,非常感谢。

标签: ruby-on-rails ruby controller routes


【解决方案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

那么在你看来你可以使用

<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

这将在location 上调用:county_id:id 并路由到类似的东西

/counties/1/locations/12

【讨论】:

    【解决方案2】:

    您是否已经尝试过类似的方法?

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

    【讨论】:

    • 您好,谢谢。我在索引中使用@location,因为我想要他们所属县下的位置列表。我唯一不能做的就是从位置列表成功链接到位置页面。所以 County_1 > List_locations_in_county_1 很好。它只是从我正在努力解决的 List_locations_in_county_1 > Individual_location_page 中获得的。知道我一定遗漏了一些明显的东西,但这似乎很难!
    • 是的,首先你可以尝试分开你的路线......不要嵌套它们......写在你的县模型has_many位置并写在你的位置模型属于县......然后尝试在你的县视图而不是你的第一行调用@county.locations.each 做...
    • 谢谢,是的,我的模型已经这样设置了 - 没有尝试调用 @county.locations.each,这是其他人的建议。除了从嵌套位置结果到位置页面的最后一个 link_to 之外,这一切都很好。
    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多