我一直在从事我在 Rails 中的第一个项目,并在使用标签时偶然发现了一个问题。我让它工作,所以当用户单击一个标签时,它会显示与该标签关联的所有条目。但是,当我尝试将这些名称链接到该条目的显示页面时,我收到此错误:
No route matches {:action=>"show", :controller=>"characters", :anime_id=>#<Character id: 1, name: "Kamina", description: "Badass", occupation: "Team Dai-Gurren", anime_id: 1, created_at: "2013-06-06 01:00:57", updated_at: "2013-06-06 03:28:20">}
我已经尝试修复它几个小时,但没有运气。如果有人对解决这个问题有一些建议,将不胜感激。
控制器:
class CharactersController < ApplicationController
def create
@anime = Anime.find(params[:anime_id])
@character = @anime.characters.create(params[:character])
redirect_to anime_path(@anime)
end
def show
@anime = Anime.find(params[:anime_id])
@character= @anime.characters.find(params[:id])
end
def index
@anime = Anime.find(params[:anime_id])
@characters= @anime.characters.all
end
def tagged
@anime = Anime.find(params[:anime_id])
if params[:tag].present?
@characters = @anime.characters.tagged_with(params[:tag])
else
@characters = @anime.characters.postall
end
end
end
路线:
Animedb::Application.routes.draw do
resources :animes do
resources :characters
end
match 'tagged' => 'characters#tagged', :as => 'tagged'
看法:
<h1>Listing Characters</h1>
<table>
<tr>
<th>Name</th>
<th>Year</th>
<th>Season</th>
<th>Genre</th>
<th></th>
<th></th>
<th></th>
<% @characters.each do |character| %>
<tr>
<td><%= link_to character.name, anime_character_path(character) %></td>
</tr>
<% end %>
</table>