【问题标题】:Ruby on Rails: Linking to resource show page results in No Route MatchesRuby on Rails:链接到资源显示页面导致无路由匹配
【发布时间】:2013-06-06 05:58:11
【问题描述】:

我一直在做我的第一个 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>

【问题讨论】:

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


    【解决方案1】:

    你需要做的:

    anime_character_path(@anime, character)
    

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

    【讨论】:

    • @MontyMonro 很高兴我能帮上忙! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多