2012-01-19 2 views
1

블로그를 구현하여 가장 많이하는 방법을 학습하고 있습니다. 태그를 추가하고 기사를 표시 할 때 클릭 가능한 태그가 표시되는 지점까지 내 기사보기를 가져 왔습니다. 문제는 링크가 이렇게 나오고 있다는 것입니다.레일 초보자 - 쿼리 스트링을 경로로 변경하는 방법

http://localhost:3000/articles?tagged_with=development

나는 쿼리 문자열을, 대신 같은 뭔가를하지 않으려는 것;

http://localhost:3000/articles/tagged_with/development

내가 (거기에 유용한 물건을 많이 그냥이!) 레일 사이트의 "경로 밖으로 내"가이드의 관련 아무것도 찾을 수 없습니다

전체 코드 여기 : https://github.com/mikeyhogarth/mikeyblog

주요 비트는;

_article.html.erb에있는 링크

:

<%= link_to tag, articles_path(:tagged_with => tag) %> 

기사 색인 컨트롤러 :

def index 

if(params[:tagged_with]) 
    @tag = params[:tagged_with] 
    @articles = Article.tagged_with @tag 
else 
    @articles = Article.all 
end 

respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @articles } 
end 
end 

이 일의 레일 모범 사례 방법은 무엇입니까? "tagged_with"액션을 구현하고 도우미를 만들거나 jiffy에서 이것을 정렬 할 수있는 레일스 라우팅 매직이 필요합니까?

편집 : 결국 나는 결국 답을 발견 대답

답변

1

보십시오 당신에

link_to(tag, tagged_articles_path(:tagged_with=>"foobar")) 
+0

롤, 아주 가까이는 :) – klochner

+0

나는 당신에게 포인트를 줄 것이다 어쨌든 새싹, 답변 시간을내어 주셔서 감사합니다. –

1

을 발견 : 나는 "라는 경로를"필요했습니다. 다른 사람이이 질문을 가지고 있다면, 나는 이것을 내 routes.rb 파일에 넣는다.

match "/articles/tagged_with/:tag" => "articles#index", :as => "articles_tagged_with" 

다음과 같이 간단하게 "link_to"를 재 배열합니다. 그런 다음

match '/articles/tagged/:tagged_with' => 'articles#index', :as => :tagged_articles 

: 귀하의 노선이 같은 (테스트되지 않은) 추가

<%= link_to tag, articles_tagged_with_path(:tag => tag) %>, 
관련 문제