Ruby on Rails:链接到资源显示页面导致无路由匹配

时间:2013-06-06 05:58:12

标签: ruby-on-rails ruby routes link-to

我一直在使用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>

1 个答案:

答案 0 :(得分:0)

你需要这样做:

anime_character_path(@anime, character)

你需要传递动漫和角色对象。