gpt4 book ai didi

ruby-on-rails - Rails 3 - 使用acts_as_taggable_on v.2.0.3 时出现路由错误

转载 作者:行者123 更新时间:2023-12-01 17:30:49 25 4
gpt4 key购买 nike

我在Rails 3 中使用acts_as_taggable_on v.2.0.3 向帖子添加标签。我添加了一个标签云,如下所述:https://github.com/mbleigh/acts-as-taggable-on ,但我遇到了一个错误:ActionController::RoutingError in Posts#index: 没有路由匹配 {:action=>"tag", :id=>"politics", :controller=>"posts"}。 我的代码如下:

PostHelper:

module PostsHelper
include TagsHelper
end

模型帖子:

class Post < ActiveRecord::Base
...
acts_as_taggable_on :tags
end

后置 Controller

class PostController < ApplicationController
...
def tag_cloud
@tags = Post.tag_counts_on(:tags)
end
end

查看:

<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>

路线.rb:

Blog::Application.routes.draw do
root :to => "posts#index"
resources :posts do
member do
post :notify_friend
end
collection do
get :search
end
resources :comments
end
resources :users
resource :session
match '/login' => "sessions#new", :as => "login"
match '/logout' => "sessions#destroy", :as => "logout"
end

我做错了什么?感谢您的回答。

最佳答案

嗯,我想我明白了。首先,我以这样的方式编辑了routes.rb:

resources :posts do
...
collection do
get :tag
end
end

其次,我在 PostController 中添加了“Tag”方法:

  def tag
@posts = Post.tagged_with(params[:id])
@tags = Post.tag_counts_on(:tags)
render :action => 'index'
end

它有效!

关于ruby-on-rails - Rails 3 - 使用acts_as_taggable_on v.2.0.3 时出现路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4174730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com