경로

2012-11-27 5 views
0

에 따라 외래 키 관계를 설정하면 여기에 지금 앉아 ...경로

class Article < ActiveRecord::Base 
    has_many :registrations 

class Registration < ActiveRecord::Base 
    belongs_to :article 

#app/views/articles/show.html.erb 
    <%= link_to 'Register', new_registration_path({:article_id => @article}) %> 

#app/views/registrations/_form.html.erb 
    <%= f.hidden_field :article_id %> 

#app/controllers/registrations_controller.rb 
def new 
    @registration = Registration.new 
    @registration.article = Article.find params[:article_id] 

내 코드입니다 .. 그리고 이것은 단지 복숭아 작동합니다. 그러나, 나는 ...

<%= link_to 'Register', new_article_registration_path(@article) %> 

을 응용 프로그램 /보기/기사/show.html.erb의 코드 없애과 경로로이 작업을 수행하고 싶습니다 ... 그리고 나는 또한 없애 싶습니다 등록보기에서 사용하고있는 hte hidden_field 중 하나입니다. 내 컨트롤러에 빌드 메소드를 추가해야한다는 것을 알고 있지만 기사 또는 등록 컨트롤러에이 메소드를 추가해야하는지 잘 모르겠습니다. 어떤 도움을 주셔서 감사합니다! 당신의 routes.rb 당신이 할 수 중첩 된 자원에서

답변

0

:

resources :articles do 
    resources :registrations 
end 

그럼 당신은 당신의 link_to 도우미의 polymorphic_url 방법의 "마법"을 사용하여 링크를 만들 수 있습니다

<%= link_to 'Register', [:new, @article, :registration] %> 
+0

고맙습니다가 jdoe를 .. <% = link_to 'Register', [: new, @article, :] %><% = link_to 'Register', new_article_registration_path (@article) %>와 (과) 동일하지 않습니까? 내 routes.rb 파일에 동일한 코드가 있습니다. 하지만 등록을 저장할 때 article_id를 매개 변수로 사용하지 않습니다. – Lumbee

+0

@ Lumbee 네, 기본적으로 같지만 더 간결합니다. 이것은 이상한 코드가 작동하지 않았다. ': articles'에 대한'resources'에서's'를 잃지 않았는지 확인하십시오. – jdoe

+0

아니요, 내 경로가 좋습니다. 내 등록 컨트롤러에 @article = Article.find params [: article_id]'또는'@registration = @ article.registrations.build '와 같은 것이 필요하다고 생각하고 있습니다. – Lumbee