2013-08-02 2 views
0

에 의해 나는 게시물 모델과 내가 뭘하려고 오전표시 모든 게시물은 카테고리

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 

재사용하는 카테고리 모델 (건조 원리를 적용) 내 응용 프로그램에서 @posts 인스턴스 변수를 사용합니다. 나는 메신저가 어딘가에서 혼란스러워하고 있다고 생각해. 각 게시물에는 자체 카테고리가 있습니다. 내보기에서

나는 모든 범주

<% @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' 
+0

와 함께이 라인

@posts = Post.where(:name => params[:name]) 

를 교체해야

def blog category_id = params[:category_id] @category = Category.find(category_id) @posts = @category.posts.order("posts.created_at DESC") end 

+0

ive가 질문 하단에 작은 업데이트를 추가했지만 꽤 직설적 인 줄 알았습니까? Ruby와 같은 카테고리 링크를 클릭하고 해당 카테고리가있는 모든 게시물을 표시합니다. – Richlewis

+0

게시물 테이블에 이름 열이없는 것 같습니다. 열을 추가하고 이전을 실행 했습니까? – Santhosh

답변

1

컨트롤러

에서

<% @categories.each do |c| %> 
<li><%= link_to c, blog_path(:category_id => c.id) %></li> 
<% end %> 

에 의해 t의 symply? :)

1

당신은 내가 할 수있는 수 당신은 아직도 당신의 문제를 언급하지 않은

category = Category.where(:name => params[:name]).first 
@posts = category.posts 
+0

을 사용해 주셔서 감사합니다. 필자는 이것을 elsif에 넣을 수 있습니까? – Richlewis

관련 문제