具有多个变量的部分
我是 Rails 新手,需要一些指导。 我确信我误解了一些明显的事情。
我希望留下评论的人的用户名显示在查看页面上。 我有一个部分负责收集特定对象(例如学校)的评论。 我不知道如何将该用户变量传递到部分中,以便我可以显示评论的登录名。 我的评论模型是多态的,我很确定这会让事情变得更加复杂。
以下是模型:
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 %>
I am new to rails and need some guidance. I am sure I am misunderstanding something obvious.
I would like the user name of the person who leaves a comment to show on the view page. I have a partial that goes through the collection of comments for the particular object for example a school. I can't figure out how to pass that user variable into the partial so that I can display the login name for the comment. My Comments model is polymorphic and I am pretty sure that is making this more complex.
Here are the models:
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
School's controller:
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
The comment partial:
<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>
The school view:
<%= render :partial => "shared/comment", :collection => @comment %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的模型可能存在一些问题,但您使用
locals
将参数传递给部分。然后在部分内部您可以访问用户变量。
I think your models might have some issues, but you pass parameters to partials using
locals
.Then inside of the partial you have access to the user variable.
例如我
for example from me