UrlGenerationError 没有路由在一台计算机上匹配,但在另一台计算机上不匹配
UrlGenerationError No route matches on one computer but not on the other
我是第一次在 Rails 上学习 Ruby,并且一直在尝试设置我的笔记本电脑以继续我在桌面上的项目。昨天我的笔记本电脑 运行 Ruby 通过 rvm 和台式机 运行 通过 rbenv 遇到了一些问题,所以我解决了这个问题。但是现在,我在我的笔记本电脑上收到以下错误,但在我的笔记本电脑上一切正常。
ActionController::UrlGenerationError in Categories#index
No route matches {:action=>"show", :controller=>"categories", :id=>nil} missing required keys: [:id]
<table>
<tr>
<th class="table-header"><%= link_to 'housing', category_path(@housing) %></th>
</tr>
<tr>
<td>
我相信它应该有效。如果我对代码的理解正确,我将通过传递 @housing 参数通过类别路径链接到我的住房类别。
正如我所说,这适用于我的桌面。
代码如下,在此先感谢您的帮助。
我的 rake routes 命令的输出以及我正在尝试使用的路由...
category GET /categories/:id(.:format) categories#show
index.html.erb
<div class="container">
<div class="leftbar">
<a href="#"><h1>rubyslist</h1></a>
<ul>
<a href="#"><%= link_to 'post a classifieds', new_listing_path %></a>
<li>my account</li>
</ul>
<p>search shiplist</p>
<%= form_tag search_listings_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
<div class="center-column" >
<div class="center-one">
<table>
<tr>
<th class="table-header"><%= link_to 'housing', category_path(@housing) %></th>
</tr>
<tr>
<td>
<% @housing.subcategories.each do |subcategory| %>
<ul class="left-list">
<li><%= link_to subcategory.name, category_subcategory_path(@housing, subcategory) %></li>
</ul>
<% end %>
</td>
</tr>
</table>
<table>
<tr>
<th class="table-header"><%= link_to 'roommates', category_path(@roommates) %></th>
</tr>
<tr>
<td>
<% @roommates.subcategories.each do |subcategory| %>
<ul>
<li><%= link_to subcategory.name, category_subcategory_path(@roommates, subcategory) %></li>
</ul>
<% end %>
</td>
</tr>
</table>
<table>
<tr>
<th class="table-header"><%= link_to 'for sale', category_path(@forsale) %></th>
</tr>
<tr>
<td>
<% @forsale.subcategories.each do |subcategory| %>
<ul class="left-list">
<li><%= link_to subcategory.name, category_subcategory_path(@forsale, subcategory) %></li>
</ul>
<% end %>
</td>
</tr>
</table>
</div>
</div>
<div class="rightbar">
<!-- Links to the pages via routes match -->
<ul class="rightbar-top">
<!-- /help -->
<li><%= link_to 'help and faq', help_path%></li>
<li><%= link_to 'avoid scams and fraud', scams_path%></li>
<li><%= link_to 'personal safety tips', safety_path%></li>
<li class="rightbar-top"><%= link_to 'terms of use', terms_path%></li>
<li><%= link_to 'privacy policy', privacy_path%></li>
<li><%= link_to 'about shiplist', about_path%></li>
<li><%= link_to 'contact us', contact_path%></li>
</ul>
</div>
</div>
routes.rb
#Creates CRUD actions for categories
resources :categories do
#Append sub-categories to categories with CRUD
resources :subcategories
end
#Performs the search
resources :listings do
collection do
get 'search'
end
end
#Set homepage to index of categories
root 'categories#index'
#Matches the pages help action to a get request
#for the help page with page controller in help
#function of pages controller
match '/help', to: 'pages#help', via: :get
match '/scams', to: 'pages#scams', via: :get
match '/safety', to: 'pages#safety', via: :get
match '/about', to: 'pages#about', via: :get
match '/contact', to: 'pages#contact', via: :get
match '/privacy', to: 'pages#privacy', via: :get
match '/terms', to: 'pages#terms', via: :get
match '/subcategories/find_by_category', to: 'subcategories#find_by_category', via: :post
end
categories_controller.rb
class CategoriesController < ApplicationController
#The index page of our categories controller
def index
@categories = Category.all
@housing = @categories[0]
@roommates = @categories[1]
@forsale = @categories[2]
end
def show
@listings = Listing.where(category_id: params[:id])
end
end
运行 命令
rake db:seed
已解决问题。
我是第一次在 Rails 上学习 Ruby,并且一直在尝试设置我的笔记本电脑以继续我在桌面上的项目。昨天我的笔记本电脑 运行 Ruby 通过 rvm 和台式机 运行 通过 rbenv 遇到了一些问题,所以我解决了这个问题。但是现在,我在我的笔记本电脑上收到以下错误,但在我的笔记本电脑上一切正常。
ActionController::UrlGenerationError in Categories#index
No route matches {:action=>"show", :controller=>"categories", :id=>nil} missing required keys: [:id]
<table>
<tr>
<th class="table-header"><%= link_to 'housing', category_path(@housing) %></th>
</tr>
<tr>
<td>
我相信它应该有效。如果我对代码的理解正确,我将通过传递 @housing 参数通过类别路径链接到我的住房类别。
正如我所说,这适用于我的桌面。
代码如下,在此先感谢您的帮助。
我的 rake routes 命令的输出以及我正在尝试使用的路由...
category GET /categories/:id(.:format) categories#show
index.html.erb
<div class="container">
<div class="leftbar">
<a href="#"><h1>rubyslist</h1></a>
<ul>
<a href="#"><%= link_to 'post a classifieds', new_listing_path %></a>
<li>my account</li>
</ul>
<p>search shiplist</p>
<%= form_tag search_listings_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
<div class="center-column" >
<div class="center-one">
<table>
<tr>
<th class="table-header"><%= link_to 'housing', category_path(@housing) %></th>
</tr>
<tr>
<td>
<% @housing.subcategories.each do |subcategory| %>
<ul class="left-list">
<li><%= link_to subcategory.name, category_subcategory_path(@housing, subcategory) %></li>
</ul>
<% end %>
</td>
</tr>
</table>
<table>
<tr>
<th class="table-header"><%= link_to 'roommates', category_path(@roommates) %></th>
</tr>
<tr>
<td>
<% @roommates.subcategories.each do |subcategory| %>
<ul>
<li><%= link_to subcategory.name, category_subcategory_path(@roommates, subcategory) %></li>
</ul>
<% end %>
</td>
</tr>
</table>
<table>
<tr>
<th class="table-header"><%= link_to 'for sale', category_path(@forsale) %></th>
</tr>
<tr>
<td>
<% @forsale.subcategories.each do |subcategory| %>
<ul class="left-list">
<li><%= link_to subcategory.name, category_subcategory_path(@forsale, subcategory) %></li>
</ul>
<% end %>
</td>
</tr>
</table>
</div>
</div>
<div class="rightbar">
<!-- Links to the pages via routes match -->
<ul class="rightbar-top">
<!-- /help -->
<li><%= link_to 'help and faq', help_path%></li>
<li><%= link_to 'avoid scams and fraud', scams_path%></li>
<li><%= link_to 'personal safety tips', safety_path%></li>
<li class="rightbar-top"><%= link_to 'terms of use', terms_path%></li>
<li><%= link_to 'privacy policy', privacy_path%></li>
<li><%= link_to 'about shiplist', about_path%></li>
<li><%= link_to 'contact us', contact_path%></li>
</ul>
</div>
</div>
routes.rb
#Creates CRUD actions for categories
resources :categories do
#Append sub-categories to categories with CRUD
resources :subcategories
end
#Performs the search
resources :listings do
collection do
get 'search'
end
end
#Set homepage to index of categories
root 'categories#index'
#Matches the pages help action to a get request
#for the help page with page controller in help
#function of pages controller
match '/help', to: 'pages#help', via: :get
match '/scams', to: 'pages#scams', via: :get
match '/safety', to: 'pages#safety', via: :get
match '/about', to: 'pages#about', via: :get
match '/contact', to: 'pages#contact', via: :get
match '/privacy', to: 'pages#privacy', via: :get
match '/terms', to: 'pages#terms', via: :get
match '/subcategories/find_by_category', to: 'subcategories#find_by_category', via: :post
end
categories_controller.rb
class CategoriesController < ApplicationController
#The index page of our categories controller
def index
@categories = Category.all
@housing = @categories[0]
@roommates = @categories[1]
@forsale = @categories[2]
end
def show
@listings = Listing.where(category_id: params[:id])
end
end
运行 命令
rake db:seed
已解决问题。