2012-11-27 2 views
2

다중 모델 폼 및 다중 모델 디스플레이를 구성하는 방법에 대한 많은 예제를 발견했습니다. 하지만 별도의 양식과 전시물을 갖고 싶다면 어떻게해야합니까?레일은 서로 다른 폼/뷰에 중첩 된 속성을 가지고 있습니다

post.rb :

class Post < ActiveRecord::Bas 
    has_many :comments, dependent: :destroy 
    attr_accessible :comments_attributes 
    accepts_nested_attributes_for :comments 
end 

comment.rb :

class Comment < ActiveRecord::Base 
    belongs_to :post 
end 

posts_controller.rb :

def new 
    @post = Post.new 
    @post.comments.build 
    ... 
end 

routes.db :

resources posts do 
    resources comments 
end 

나는 내 게시물 지수에 의견 지수를 게시 할 수있는 링크가 있습니다

보기/게시물/index.html.erb :

... 
<%= link_to 'Comments', post_comments_path(post) %> 
... 

포스트와 (중첩되지 않음) 각각 자신의 발판이 생성 한 양식을 코멘트 . 게시물 댓글을 통해 의견 인덱스 I 루프에서

<%= form_for(@post) do |f| %> 
... 

<%= form_for(@comment) do |f| %> 
... 

:

보기/의견/index.html.erb :

<% @post = Post.find(params[:post_id]) %> //works fine 
<% @post.comments.each do |comment| %> 
... 
<% end %> 

그러나 특정 게시물의 ID로 새로운 코멘트를 (추가 한 후) 게시물 코멘트 인덱스에있는 테이블은 입니다!

도와주세요. 감사 :

답변

1

나는 그것을 알아 냈다. 주석 형태

는 같아야

<%= form_for([@post, @comment]) do |f| %> 
... 

경로 같이 표기 :

def index 
    @post= Post.find(params[:post_id]) 
    @comments= @post.comments.all 
... 

def show 
    @post= Post.find(params[:post_id]) 
    @comment= @post.comments.find(params[:id]) 
... 

: 주석 컨트롤러

post_comments_path(@post) 
edit_post_comment_path(@post,@comment) 

다른 사람들이 유용하다고 생각합니다!

+0

색인보기에서이 오류가 발생합니다 ... ID가없는 을 찾을 수 없습니다. – roadev

관련 문제