我是Ruby on Rails(和編程)的新手,這可能是一個非常愚蠢的問題。我正在使用Rails 3.2並嘗試使用acts_as_taggable_on在文章上生成標籤,並將這些標籤顯示爲文章索引和顯示頁面作爲可點擊鏈接。如何獲得標籤工作行爲
我有標籤可點擊文章展示和索引頁面,但鏈接只是回到索引頁面,不根據標籤名稱進行排序。我搜索了互聯網,並將各種來源的代碼拼湊在一起,但我顯然錯過了一些東西。
任何幫助非常感謝,因爲我已經用盡了我看似有限的知識!謝謝。
以下是我有:
class ArticlesController < ApplicationController
def tagged
@articles = Article.all(:order => 'created_at DESC')
@tags = Article.tag_counts_on(:tags)
@tagged_articles = Article.tagged_with(params[:tags])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @articles }
end
end
def index
@articles = Article.paginate :page => params[:page], :per_page => 3
@tags = Article.tag_counts_on(:tags)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @articles }
end
end
module ArticlesHelper
include ActsAsTaggableOn::TagsHelper
end
class Article < ActiveRecord::Base
acts_as_ordered_taggable
acts_as_ordered_taggable_on :tags, :location, :about
attr_accessible :tag_list
scope :by_join_date, order("created_at DESC")
end
article/index.html.erb
<% tag_cloud(@tags, %w(tag1 tag2 tag3 tag4)) do |tag| %>
<%= link_to tag.name, articles_path(:id => tag.name) %>
<% end %>
article/show.html.erb
<%= raw @article.tags.map { |tag| link_to tag.name, articles_path(:tag_id => tag) }.join(" | ") %>
的routes.rb文件片段
authenticated :user do
root :to => 'home#index'
end
devise_for :users
resources :users, :only => [:show, :index]
resources :images
resources :articles
請爲您的路線文件 – 2012-07-08 03:25:10
添加一個片段,爲什麼您的索引中有@article = Article.new? – 2012-07-08 03:27:06
編輯:添加路線片段和刪除** @ article = Article.new **從控制器中的** def index **(不記得它爲什麼在那裏,我一直在複製/粘貼和製造誤會...... ) – Schipperius 2012-07-10 01:56:07