我有routes.rb
city_section_slug GET (/:locale)/cities/:city_id/:section(.:format) cities#show {:locale=>/ru|en/}
city_subsection_slug GET (/:locale)/cities/:city_id/:section/:subsection(.:format) cities#show {:subsection=>/[^\/]*/, :locale=>/ru|en/}
rake显示这些路线
= link_to city_subsection_slug_path(@city,section.alias, subsection.alias)
但是当我尝试创建链接时:
ActionController::RoutingError (No route matches {:controller=>"cities", :action=>"show", :locale=>:ru, :city=>#<City id: 42, name: "City">, :section=>"events", :subsection=>"sobytiya/ya-ochevidets"}):
我遇到了这样的错误:
$data = [
400 => [
'name' => 'x',
'lesson_1' => '10',
'lesson_2' => '9',
...
],
389 => ...
]
我错的任何想法?
答案 0 :(得分:0)
#config/routes.rb
resources :cities, only: [] do
get ":section", action: :show, as: :city_section_slug #-> url.com/cities/:city_id/:section
get ":section/*subsection", action: :show, as: :city_subsection_slug #-> url.com/cities/:city_id/:section/:subsection
end
相应的链接:
<%= link_to "x", city_section_slug_path(@city, section.alias) %>
<%= link_to "x", city_subsection_slug_path(@city, section.alias, subsection.alias) %>
<强>通配符强>
如果您希望将sobytiya/ya-ochevidets
发送到subsection
路径,那么您最好使用wildcard路径
答案 1 :(得分:0)
原因是你对subsection param的约束。您指定它可以是任何内容,只要它不包含/
个字符。你的小节确实是因为错误。如果您想允许/
个字符,请执行以下操作:
get '/cities/:city/:section/:subsection' => 'cities#show',
as: 'city_subsection_slug',
constraints: { :subsection => /^[a-z0-9_\/]+$/ }
最终,你可以使用glob param:
get '/cities/:city/:section/*subsection' => 'cities#show',
as: 'city_subsection_slug',