具有多个变量的部分

发布于 2024-08-01 16:20:48 字数 1370 浏览 10 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-08-08 16:20:48

我认为您的模型可能存在一些问题,但您使用 locals 将参数传递给部分。

<%= render :partial => "shared/comment", :locals => { :user => your_user } %>

然后在部分内部您可以访问用户变量。

<p><span class="stars"><%= display_stars(comment.stars) %></span><br />
<%= comment.content %><br />
by <%= user %></p>

I think your models might have some issues, but you pass parameters to partials using locals.

<%= render :partial => "shared/comment", :locals => { :user => your_user } %>

Then inside of the partial you have access to the user variable.

<p><span class="stars"><%= display_stars(comment.stars) %></span><br />
<%= comment.content %><br />
by <%= user %></p>
夏末的微笑 2024-08-08 16:20:48

例如我

def show 
@school = School.find_by_id_and_user_id(params[:id],
params[:user_id],:include => [:user, [:comments => :user]])
end

class Comment < ActiveRecord::Base
  belongs_to :school, :counter_cache => true
  belongs_to :user
end

class School < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  has_many :comments

  def after_save
   self.user.update_attribute(:last_activity, "bla bla bla")
   self.user.update_attribute(:last_activity_at, Time.now)
  end
end

 class User
   has_many :school or has_one
   has_many :comments
 end

<div class="box-one">
<img src="/images/comment_top.png" alt="" />
<div class="inside">
        <% @schools.comments.each do |comment| -%>
        <%= comment.created_at.to_s(:short) %>  
        <p><span class="stars"><%= <%= comment.user.stars %> said: ## or<%= comment.user.username %> said:##</p> %></span><br />
<%= comment.content %></p>  

</div>
<img src="/images/comment_bottom.png" alt="" />
</div>

<%= render :partial => "shared/comment"%>

for example from me

def show 
@school = School.find_by_id_and_user_id(params[:id],
params[:user_id],:include => [:user, [:comments => :user]])
end

class Comment < ActiveRecord::Base
  belongs_to :school, :counter_cache => true
  belongs_to :user
end

class School < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  has_many :comments

  def after_save
   self.user.update_attribute(:last_activity, "bla bla bla")
   self.user.update_attribute(:last_activity_at, Time.now)
  end
end

 class User
   has_many :school or has_one
   has_many :comments
 end

<div class="box-one">
<img src="/images/comment_top.png" alt="" />
<div class="inside">
        <% @schools.comments.each do |comment| -%>
        <%= comment.created_at.to_s(:short) %>  
        <p><span class="stars"><%= <%= comment.user.stars %> said: ## or<%= comment.user.username %> said:##</p> %></span><br />
<%= comment.content %></p>  

</div>
<img src="/images/comment_bottom.png" alt="" />
</div>

<%= render :partial => "shared/comment"%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文