我是 Rails 新手,需要一些指导。我确定我误解了一些明显的东西。
我希望发表评论的人的用户名显示在查看页面上。我有一个部分通过对特定对象(例如学校)的评论收集。我不知道如何将该用户变量传递给部分,以便我可以显示评论的登录名。我的 Comments 模型是多态的,我很确定这会使它变得更加复杂。
以下是模型:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
has_many :comments, :through => :schools
class School < ActiveRecord::Base
belongs_to :installation
belongs_to :neighborhood
has_many :comments, :as => :commentable
class User < ActiveRecord::Base
has_many :users, :through => :schools
学校负责人:
def show
@installation = Installation.find(params[:installation_id])
@school = School.find(params[:id])
@neighborhood = @school.neighborhood
@comment = @school.comments
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @school }
end
end
部分评论:
<div class="box-one">
<img src="/images/comment_top.png" alt="" />
<div class="inside">
<p><span class="stars"><%= display_stars(comment.stars) %></span><br />
<%= comment.content %></p>
</div>
<img src="/images/comment_bottom.png" alt="" />
</div>
学校观点:
<%= render :partial => "shared/comment", :collection => @comment %>