我有一個職位模型,並用我的應用程序的@posts實例變量當一個分類模型顯示所有帖子的
class Category < ActiveRecord::Base
has_many :posts
attr_accessible :name
end
Class Post < ActiveRecord::Base
belongs_to :category
attr_accessible :comments, :title, :category_id, :user_id, :photo
end
我所試圖做的是再利用(應用DRY原則)。我想我會陷入困境。每個帖子都有自己的類別。
在我看來,我有所有的類上市
<% @categories.each do |c, v| %>
<li><%= link_to c, blog_path(:name => c) %></li>
<% end %>
控制器
def blog
if params[:month]
date = Date.parse("1 #{params[:month]}") # to get the first day of the month
@posts = Post.where(:created_at => date..date.end_of_month) # get posts for the month
elsif params[:name]
@posts = Post.where(:name => params[:name])
else
@posts = Post.all(:order => "created_at DESC")
end
@latest = Post.latest_posts
@posts_by_month = Post.all.group_by { |post| post.created_at.strftime("%B %Y") }
#Category Section down side of page
@categories = Category.all.group_by { |c| c.name }
end
我想要實現的是點擊一個類別,然後它會顯示屬於該類別的所有訊息,目前當我點擊一個分類鏈接時我得到
Mysql2::Error: Unknown column 'posts.name' in 'where clause': SELECT `posts`.* FROM `posts` WHERE `posts`.`name` = 'Ruby'
你還沒有說你的問題牛逼symply? :) –
我添加了小問題底部的更新,但我認爲這是非常簡單的?我想點擊一個分類鏈接,即紅寶石,然後它會顯示所有具有該類別的帖子 – Richlewis
看起來你的帖子表沒有名稱列。您是否添加了該柱並運行遷移? – Santhosh