act-as-taggable-on的URL帮助器

时间:2014-11-06 16:20:32

标签: ruby-on-rails

以下代码无效

<p>Tags: <%= @video.tags.map { |t| link_to t.name, tag_path(t)}.join(', ') %></p>

Rails抱怨undefined method 'tag_path' for #<#<Class:0x007fd0f0260890>:0x007fd0ebce5f68>

我认为tag_path不是正确的url_helper,任何人都可以解释在这里使用哪一个?

我的rake routes

              video_tags GET    /videos/:video_id/tags(.:format)                  tags#index
                     POST   /videos/:video_id/tags(.:format)                  tags#create
       new_video_tag GET    /videos/:video_id/tags/new(.:format)              tags#new
      edit_video_tag GET    /videos/:video_id/tags/:id/edit(.:format)         tags#edit
           video_tag GET    /videos/:video_id/tags/:id(.:format)              tags#show
                     PATCH  /videos/:video_id/tags/:id(.:format)              tags#update
                     PUT    /videos/:video_id/tags/:id(.:format)              tags#update
                     DELETE /videos/:video_id/tags/:id(.:format)              tags#destroy
              videos GET    /videos(.:format)                                 videos#index
                     POST   /videos(.:format)                                 videos#create
           new_video GET    /videos/new(.:format)                             videos#new
          edit_video GET    /videos/:id/edit(.:format)                        videos#edit
               video GET    /videos/:id(.:format)                             videos#show
                     PATCH  /videos/:id(.:format)                             videos#update
                     PUT    /videos/:id(.:format)                             videos#update
                     DELETE /videos/:id(.:format)                             videos#destroy

除此之外,我没有改变我的routes.rb,它看起来像这样:

Rails.application.routes.draw do

  devise_for :users
  get 'welcome/index'

  root 'welcome#index'

  resources :articles do
    resources :comments
  end

  resources :videos do
    resources :tags
  end

end

1 个答案:

答案 0 :(得分:3)

好吧,我这样做了:

为routes.rb添加了资源:

  resources :videos do
    resources :tags
  end

设置路由后,我将RHTML更改为:

<p>
Tags: 
  <%= raw @video.tags.map {
        |t| link_to t.name, video_tag_path(:video_id => @video.id, :id => t.id)
    }.join(', ') %>
</p>

必须明确指定video_id和tag id参数。

对于标签,您必须在之后生成控制器。