2011-11-05 62 views
1

我目前在我的應用程序中使用以下gem來提供分頁,搜索,可鏈接標記和標記雲。標記鏈接不起作用作爲taggable和Rails 3.1

寶石 'will_paginate', '〜> 3.0.2' 寶石 '的作用,爲-加標籤式', '〜> 2.1.0'

我所擁有的一切,到目前爲止實現對Rails 3.1的除外標記鏈接。我將它們顯示在每篇文章下方和標籤雲中,並且都鏈接到不同視圖,但都不起作用。

標籤雲(測試1)的鏈接鏈接我:/職位/標籤ID = Test1的,給了我一個錯誤「無法使用id =標籤找到郵報」

而且在一個鏈接?帖子將我重定向到/ posts?tag = Test2 & view =標籤不會改變任何東西。我的所有帖子仍然顯示。

這是我到目前爲止已經改變,試圖實現這一點:

我的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小時試圖弄清楚這一點,但沒有什麼是最新的或完全相關。

我得到它正在將「標記」傳遞給show動作,但是如何獲取它以傳遞相關的ID和其他任何我需要用該標記顯示正確的帖子?

+0

這是一個路由問題。運行'rake routes'並將相關部分複製/粘貼到您的問題中(所有包含':controller =>「posts」''的行) – Thilo

+0

謝謝,這裏是:http://pastebin.com/sDQRrUwk另外我試試這個:match「/ post /:tag」=>「posts#show」,:as =>:tag,它仍然不會匹配我的後控制器中的標記動作。 – robertlong

+0

另外我想我需要添加:通過=>:到達路線,但是當我這樣做時,它仍然不起作用。我正在犯一些簡單的錯誤,但我無法弄清楚。 – robertlong

回答

1

愚蠢的錯誤:

我沒有通過p嘲諷標籤定義中的@posts數組。

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

工作完美。感謝您的幫助,您是解決方案的一部分。

0

看着你rake routes輸出,這裏是你的問題:

 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 route輸出應該是這樣的:

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) 
+0

好的路由錯誤已修復。我將route.rb改爲:'match'/ posts/tag「=>」posts#tag「,:as =>:tag_posts resources:posts,:sessions'但是現在鏈接不會搜索帶有關聯標籤。我的帖子控制器中的標籤定義有什麼問題嗎? – robertlong

+0

這可能是這個<%= link_to tag.name,{:action =>:tag,:id => tag.name},:class => css_class%>中的動作,我怎樣才能從它返回結果標籤定義? – robertlong

+0

修改我的答案。 – Thilo