嗨,我是铁杆的新手,无法弄清楚我做错了什么我在这里搜索Rails Routing但似乎没有什么对我有用。任何帮助将不胜感激。
所以我在Categories#show中得到了这个错误ActionController :: UrlGenerationError。使用以下No route matches {:action=>"show", :controller=>"categories", :id=>nil} missing required keys: [:id]
以下是显示错误的代码。
<a href=""><%= link_to 'Adoption', category_path(@adoption) %></a>
这是我的佣金路线输出
category_subcategories GET /categories/:category_id/subcategories(.:format) subcategories#index
POST /categories/:category_id/subcategories(.:format) subcategories#create
new_category_subcategory GET /categories/:category_id/subcategories/new(.:format) subcategories#new
edit_category_subcategory GET /categories/:category_id/subcategories/:id/edit(.:format) subcategories#edit
category_subcategory GET /categories/:category_id/subcategories/:id(.:format) subcategories#show
PATCH /categories/:category_id/subcategories/:id(.:format) subcategories#update
PUT /categories/:category_id/subcategories/:id(.:format) subcategories#update
DELETE /categories/:category_id/subcategories/:id(.:format) subcategories#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
我的类别以及嵌套在我的侧边栏中的子类别。即使我将它从application.html.erb移到index.html.erb,我仍然会遇到同样的错误。
application.html.erb
<div class="related">
<h3>Check Out</h3>
<br>
<a href=""><%= link_to 'Adoption', category_path(@adoption) %></a>
<br>
<% @adoption.subcategories.each do |subcategory| %>
<ul>
<li><%= link_to subcategory.name %></li>
</ul>
<% end %>
categories_controller
def index = Category.all
@adoption = @categories[0]
@missing = @categories[1]
@silly = @categories[2]
end
def show
end
routes.rb 只是加入
resources :categories do
resources :subcategories
end
root 'categories#index'
end
感谢大家和任何能够帮助我的人。
答案 0 :(得分:1)
修复您的控制器以填写所有操作的@categories
:
before_action :set_categories
def index
end
def show
end
private
def set_categories
@categories = Category.all
@adoption = @categories[0]
@missing = @categories[1]
@silly = @categories[2]
end
为{em>任何操作呈现了您的application.html.erb
,因此您应该为任何操作填写类别。如果您只想在与类别相关的网页上呈现类别,请将代码从application.html.erb
移至categories.html.erb
。