如何捕获评论的作者,然后在工具提示中显示? [英] How to capture a comment's author then display in tooltip?

查看:140
本文介绍了如何捕获评论的作者,然后在工具提示中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单附加到可以提交简短评论的个人资料。我想捕获作者的名字,所以当鼠标悬停在注释的正文上时,我可以在工具提示中显示它。



在我的创建方法在控制器我有:

  def create 
@comment = Comment.new(params [ :comment])
@ comment.save!
redirect_to profile_path(@ comment.profile)
end

  t.timestamps 
t.integer:profile_id
t.string:author_id
t .string:body

个人资料模式:

  belongs_to:user 
accepts_nested_attributes_for:user
has_many:comments


b $ b

评论模型:

  belongs_to:profile 

ProfilesController:

  def show 
@user = User.find(params [:id])
@profile = user.profile
@superlative = @ profile.superlatives.new
end

而我的形式:

 <%= form_for @comment do | f | %> 
<%= f.hidden_​​field:profile_id,:value => @ profile.id%>
<%= f.hidden_​​field:author_id,:value => #{current_user.profile.first_name}#{current_user.profile.last_name}%>
<%= f.text_field:body%>
<%= f.submit'添加新的'%>
<%end%>

我想链接:author_id到current_user.profile.id并使用该关联显示: first_name和:last_name,它们是配置文件的属性。或者有更简单,更好的方法吗?



更新:我得到它显示的名称,虽然我还是好奇,如果有一个更好方法。

解决方案

您的解决方案看起来不错,但我会存储 User > $ b

app / models / comment.rb

  class Comment< ActiveRecord :: Base 

belongs_to:profile
belongs_to:author,:class_name => User,:foreign_key => author_id

...其余代码...

end

然后将您的迁移更改为:

  t.integer:author_id 

和您的控制器方法:

  def create 
@comment = Comment.new(params [:comment] .merge(:author_id => current_user.id))
@ comment.save!
redirect_to profile_path(@ comment.profile)
end

我使用 title 属性创建一个工具提示,但随意使用任何你喜欢的方法):

 < div class =commenttitle =<%= @ comment.author.profile.first_name%><%= @ comment.author.profile.last_name%> > 
<%= @ comment.body%>
< / div>


I have a form attached to profiles where short comments can be submitted. I want to capture the author's name though so I can display it in a tooltip when hovering over the comment's body.

In my create method in the controller I have:

def create
  @comment = Comment.new(params[:comment])
  @comment.save!
  redirect_to profile_path(@comment.profile)
end

Inside my migration:

t.timestamps
t.integer :profile_id
t.string :author_id
t.string :body

Profile model:

belongs_to :user
accepts_nested_attributes_for :user
has_many :comments

Comment model:

belongs_to :profile

ProfilesController:

def show
  @user = User.find(params[:id])
  @profile = user.profile
  @superlative = @profile.superlatives.new
end

And my form:

<%= form_for @comment do |f| %>
  <%= f.hidden_field :profile_id, :value => @profile.id %>
  <%= f.hidden_field :author_id, :value => "#{current_user.profile.first_name} #{current_user.profile.last_name}" %>
  <%= f.text_field :body %>
  <%= f.submit 'Add new' %>
<% end %>

I was thinking of linking the :author_id to current_user.profile.id and using that association to display :first_name and :last_name which are attributes of the profile. Or is there a simpler, better way?

UPDATE: I got it to display the name though I'm still curious if there's a better way.

解决方案

Your solution looks fine, but I'd store the User (or whatever class current_user returns) instead of the Profile:

In app/models/comment.rb:

class Comment < ActiveRecord::Base

  belongs_to :profile
  belongs_to :author, :class_name => "User", :foreign_key => "author_id"

  ... rest of the code ...

end

You then change your migration to:

t.integer :author_id

and your controller method to:

def create
  @comment = Comment.new(params[:comment].merge(:author_id => current_user.id))
  @comment.save!
  redirect_to profile_path(@comment.profile)
end

In your view (I used the title attribute do create a tooltip, but feel free to use whatever method you like):

<div class="comment" title="<%= @comment.author.profile.first_name %> <%= @comment.author.profile.last_name %>">
  <%= @comment.body %>
</div>

这篇关于如何捕获评论的作者,然后在工具提示中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆