2

私はacts_as_taggable_onステロイドを使用していますが、タグへのリンクを生成するこのコードに問題があります:

<%= link_to tag, tag_path(:id => tag.name) %>

URLにアクセスすると:

http://localhost:3000/tags/rails

エラーが発生します:

No action responded to rails. Actions: show

ただし、この URL は機能します。

http://localhost:3000/tags/show/rails

tags_controller.rb で show アクションを定義しました

class TagsController < ApplicationController
  def show
    @stories = Story.find_tagged_with(params[:id])
  end
end

rake:routes によって生成された次のルートがあります。

           tags GET    /tags(.:format)                             {:controller=>"tags", :action=>"index"}
                POST   /tags(.:format)                             {:controller=>"tags", :action=>"create"}
        new_tag GET    /tags/new(.:format)                         {:controller=>"tags", :action=>"new"}
       edit_tag GET    /tags/:id/edit(.:format)                    {:controller=>"tags", :action=>"edit"}
            tag GET    /tags/:id(.:format)                         {:controller=>"tags", :action=>"show"}
                PUT    /tags/:id(.:format)                         {:controller=>"tags", :action=>"update"}
                DELETE /tags/:id(.:format)                         {:controller=>"tags", :action=>"destroy"}

URLタグ/レールがルートtags/:idを指していることはわかっています.link_toに追加のパラメータを提供して、タグ名を:idパラメータとして割り当てましたが、ご覧のとおり、機能していません. フォーラムでは to_param を使用するよう提案されましたが、Tag モデルがなく、本ではそれに対して提案されていません。何か不足していますか?

私はSitepointの本Simply Rails 2に従っています

編集:作業 URL を追加、上部を参照

4

4 に答える 4

0

これをルート リソースに追加してみてください。

:requirements => { :id => /.*/ }
于 2011-02-17T21:47:11.837 に答える
0

私にとっては、routes.rbの違いのように見えます

resources :tags

resource :tags

1 つ目はデフォルトの index アクションを持ち、2 つ目は :index を持ちませんが、デフォルト ルートでの表示で応答します。

于 2011-02-24T20:53:07.010 に答える
0

ここで暗い場所で撮影する必要があります

<%= link_to tag, tag_path(:id => tag.name) %>

なれ

<%= link_to tag, tag_path(:id => tag.id) %>

また

<%= link_to tag, tag_path(tag) %>

于 2011-02-20T06:03:14.973 に答える
0

あなたのリンクのためにこれを試してください:

link_to tag.name, { :action => :tag, :id => tag.name }

使用しているレールのバージョンはわかりませんが、3 を想定しています。

基本的に、id から外れる tag_path を使用しています。何も変更していない場合はtag/43、ID 43 のタグなどを意味しto_paramますtag/rails。そのためには、次のようにします。

class Tag
  def to_param
    name
  end
end

最後に、ID ではなく名前を使用するように show アクションを変更する必要があります。だから@stories = Story.find_tagged_with(params[:name])。次に、これを補うためにルートを作成したいと思うので、 の上にresources :tagsを追加しmatch "/tags/:name" => "tags#show"ます。

于 2011-02-20T06:20:52.813 に答える