标签链接无法在 Rails 3.1 上充当可标记的角色

发布于 2024-12-13 23:22:15 字数 2418 浏览 2 评论 0原文

我目前在我的应用程序中使用以下 gem 来提供分页、搜索、可链接标签和标签云。

gem 'will_paginate', '~>; 3.0.2' gem 'acts-as-taggable-on', '~>2.1.0'

到目前为止,除了标签链接之外,我已经在 Rails 3.1 中实现了所有内容。我将它们显示在每个帖子下方和标签云中,并且都链接到不同的视图,但都不起作用。

标签云 (Test1) 中的链接将我链接到:/posts/tag?id=Test1,这给了我一个错误“无法找到带有 id=tag 的帖子”

并且帖子中的链接将我重定向到 /posts?tag =Test2&view=tag 不会改变任何东西。我的所有帖子仍然显示。

以下是我到目前为止为尝试实现此目的所做的更改:

我的routes.rb的编辑部分:

resources :posts do
collection do
  get :tag
end

结束

我的post.rb:

acts_as_taggable_on :tags
def self.tag_post_list(page, tag)

  Post.tagged_with(tag).by_date.paginate(:page => params[page], :per_page => 20)

end

Post_Controller.rb:

def index
  @posts = Post.search(params[:search], params[:page])
  @tags = Post.tag_counts_on(:tags)
  if request.xhr?
    render :partial => @posts
  end
end
def tag
  @posts = Post.tagged_with(params[:id])
  @tags = Post.tag_counts_on(:tags)
  render :action => 'index'
end
def show
  @post = Post.find(params[:id])
  @tags = Post.tag_counts_on(:tags)

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @post }
  end
end

Posts_helper.rb:

include ActsAsTaggableOn::TagsHelper

application.html.erb中的标签云

<% 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 %>

application_controller中的标签云定义.rb

def tag_cloud
  @tags = Post.tag_counts_on(:tags)
end

我的帖子中的链接:

<% for tag in post.tags %>
<%= link_to tag.name, posts_path(:view =>'tag', :tag => tag.name) %>
<% end %>

我的控制台中的错误:

Started GET "/posts/tag?id=Test1" for 127.0.0.1 at 2011-11-05 00:35:00 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"tag"}
Post Load (0.0ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", "tag"]]
Completed 404 Not Found in 3ms

ActiveRecord::RecordNotFound (Couldn't find Post with id=tag): app/controllers/posts_controller.rb:33:in `show'

过去 5 小时我在互联网上搜索试图找出答案,但没有任何内容是最新的或完全相关的。

我知道它正在将“标签”传递给显示操作,但是如何让它传递相关 ID 以及我需要的其他内容来显示带有该标签的正确帖子?

I'm currently using the following gems in my application to provide pagination, search, linkable tags, and a tag cloud.

gem 'will_paginate', '~> 3.0.2'
gem 'acts-as-taggable-on', '~>2.1.0'

I have everything implemented so far in Rails 3.1 except the tag links. I have them displayed below every post and in the tag cloud and both link to different views and neither works.

A link in the tag cloud (Test1) links me to: /posts/tag?id=Test1 which gives me an error "Couldn't find Post with id=tag"

And a link in the post redirects me to /posts?tag=Test2&view=tag which doesn't change anything. All my posts are still displayed.

Here's what I've changed so far to try and implement this:

The edited part of my routes.rb:

resources :posts do
collection do
  get :tag
end

end

My post.rb:

acts_as_taggable_on :tags
def self.tag_post_list(page, tag)

  Post.tagged_with(tag).by_date.paginate(:page => params[page], :per_page => 20)

end

Post_Controller.rb:

def index
  @posts = Post.search(params[:search], params[:page])
  @tags = Post.tag_counts_on(:tags)
  if request.xhr?
    render :partial => @posts
  end
end
def tag
  @posts = Post.tagged_with(params[:id])
  @tags = Post.tag_counts_on(:tags)
  render :action => 'index'
end
def show
  @post = Post.find(params[:id])
  @tags = Post.tag_counts_on(:tags)

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @post }
  end
end

Posts_helper.rb:

include ActsAsTaggableOn::TagsHelper

Tag cloud in application.html.erb

<% 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 %>

Tag cloud definition in application_controller.rb

def tag_cloud
  @tags = Post.tag_counts_on(:tags)
end

Links in my posts:

<% for tag in post.tags %>
<%= link_to tag.name, posts_path(:view =>'tag', :tag => tag.name) %>
<% end %>

The error in my console:

Started GET "/posts/tag?id=Test1" for 127.0.0.1 at 2011-11-05 00:35:00 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"tag"}
Post Load (0.0ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", "tag"]]
Completed 404 Not Found in 3ms

ActiveRecord::RecordNotFound (Couldn't find Post with id=tag): app/controllers/posts_controller.rb:33:in `show'

I've searched the internet for the past 5 hours trying to figure this out, but nothing is up to date or entirely relevant.

I get that it is passing "tag" to the show action, but how do I get it to pass the relevant ID and anything else I need to show the correct posts with that tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-12-20 23:22:15

愚蠢的错误:

我没有将分页传递给标签定义中的@posts数组。

@posts = Post.tagged_with(params[:id]).paginate(:page => params[:page], :per_page => 10)

工作很完美。感谢您的帮助,您是解决方案的一部分。

Stupid mistake:

I wasn't passing paginate to the @posts array in the tag definition.

@posts = Post.tagged_with(params[:id]).paginate(:page => params[:page], :per_page => 10)

Work's perfectly. Thanks for the help, you were part of the solution.

别忘他 2024-12-20 23:22:15

查看您的 rake paths 输出,这是您的问题:

     post GET    /posts/:id(.:format)         {:action=>"show", :controller=>"posts"}
    ...
tag_posts GET    /posts/tag(.:format)         {:action=>"tag", :controller=>"posts"}

URL /posts/tag?id=Test1 与这两个路由匹配,因此使用第一个,导致调用 posts#show 操作,而不是按预期调用 posts#tag 操作。

现在我不太确定为什么会这样 - 你似乎为帖子控制器定义了太多路由。除了问题中的条目之外,您的路线文件中的某处是否还有其他 posts 条目?

这样:

resources :posts do
  collection do
    get :tag
  end
end

您的 rake 路由输出应如下所示:

tag_posts GET    /posts/tag(.:format)       {:action=>"tag", :controller=>"posts"}
    posts GET    /posts(.:format)           {:action=>"index", :controller=>"posts"}
          POST   /posts(.:format)           {:action=>"create", :controller=>"posts"}
 new_post GET    /posts/new(.:format)       {:action=>"new", :controller=>"posts"}
edit_post GET    /posts/:id/edit(.:format)  {:action=>"edit", :controller=>"posts"}
     post GET    /posts/:id(.:format)       {:action=>"show", :controller=>"posts"}
          PUT    /posts/:id(.:format)       {:action=>"update", :controller=>"posts"}
          DELETE /posts/:id(.:format)       {:action=>"destroy", :controller=>"posts"}

这里 tag_post 路由是在默认 post 路由之前定义的,这将修复你的问题。

编辑

根据您的评论,稍微更改您添加的路由:

match "/posts/tag/:id" => "posts#tag", :as => :tag_posts

然后在链接中使用命名路由:

link_to tag.name, tag_posts_path(tag.name)

Looking at your rake routes output, here's your problem:

     post GET    /posts/:id(.:format)         {:action=>"show", :controller=>"posts"}
    ...
tag_posts GET    /posts/tag(.:format)         {:action=>"tag", :controller=>"posts"}

the URL /posts/tag?id=Test1 is matched by both these routes, so the first one is used, leading to the posts#show action being called instead of the posts#tag action as intended.

Now I'm not quite sure why that is - you seem to have too many routes defined for the posts controller. Do you have any other entries for posts somewhere in your routes file in addition to the one you have in your question?

With just this:

resources :posts do
  collection do
    get :tag
  end
end

your rake route output should look like this:

tag_posts GET    /posts/tag(.:format)       {:action=>"tag", :controller=>"posts"}
    posts GET    /posts(.:format)           {:action=>"index", :controller=>"posts"}
          POST   /posts(.:format)           {:action=>"create", :controller=>"posts"}
 new_post GET    /posts/new(.:format)       {:action=>"new", :controller=>"posts"}
edit_post GET    /posts/:id/edit(.:format)  {:action=>"edit", :controller=>"posts"}
     post GET    /posts/:id(.:format)       {:action=>"show", :controller=>"posts"}
          PUT    /posts/:id(.:format)       {:action=>"update", :controller=>"posts"}
          DELETE /posts/:id(.:format)       {:action=>"destroy", :controller=>"posts"}

Here the tag_post route is defined prior to the default post route, which would fix your issue.

EDIT:

Based on your comment, change the route you added slightly to this:

match "/posts/tag/:id" => "posts#tag", :as => :tag_posts

and then use the named route in your link:

link_to tag.name, tag_posts_path(tag.name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文