2
기사 및 뉴스 페이지가있는 사이트를 개발하고 있으며 모두 의견을 말할 수있는 기회를 추가하고 싶습니다. 나는 다형성 연관을 사용한다.Ruby on Rails. 다형성 연관의 뷰
class Article < ActiveRecord::Base
has_many :commentaries, :as => :commentable
end
class News < ActiveRecord::Base
has_many :commentaries, :as => :commentable
end
class Commentary < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
나는 commentable 객체
보기/기사/show.html.erb 아래 주석을 보여 드리고자합니다
<p>
<b>Title:</b>
<%= @article.title %>
</p>
<p>
<b>Short text:</b>
<%= @article.short_text %>
</p>
<p>
<b>Full text:</b>
<%= @article.full_text %>
</p>
<%= render 'commentaries/form' %>
보기/뉴스/show.html.erb
<p>
<b>Title:</b>
<%= @news.title %>
</p>
<p>
<b>Text:</b>
<%= @news.text %>
</p>
<p>
<b>Created:</b>
<%= @news.created %>
</p>
뷰/설명/_form.html.erb
(210)<h1>Comments</h1>
<ul id="comments">
<% @commentaries.each do |comment| %>
<li><%= comment.content %></li>
<% end %>
</ul>
<h2>New Comment</h2>
<% form_for [@commentable, Comment.new] do |form| %>
<ol class="formList">
<li>
<%= form.label :content %>
<%= form.text_area :content, :rows => 5 %>
</li>
<li><%= submit_tag "Add comment" %></li>
</ol>
<% end %>
그리고 내 컨트롤러 : 나는 내 사이트/기사에 갈 때
class CommentariesController < ApplicationController
def index
@commentable = find_commentable
@commentaries = @commentable.commentaries
end
end
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
end
end
이/한 내가 오류가 정의되지 않은 메서드`각 '무기 호에 대한 얻을 :이 있기 때문에 내 문서 컨트롤러 @commentable, NilClass을하지 않으며 해설 컨트롤러의 코드는 실행되지 않습니다.
기사/쇼 페이지에서 주석 컨트롤러의 색인 작업을 실행하는 방법은 무엇입니까? 논평 양식을
<%= render 'commentaries/form', :commentable => @article %>
액세스 당신의 지역 변수 부분 뷰를 렌더링하는 동안