如何捕获评论的作者,然后在工具提示中显示? [英] How to capture a comment's author then display in tooltip?
问题描述
我有一个表单附加到可以提交简短评论的个人资料。我想捕获作者的名字,所以当鼠标悬停在注释的正文上时,我可以在工具提示中显示它。
在我的创建
方法在控制器我有:
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,它们是配置文件的属性。或者有更简单,更好的方法吗?
更新:我得到它显示的名称,虽然我还是好奇,如果有一个更好方法。
您的解决方案看起来不错,但我会存储 在 然后将您的迁移更改为: 和您的控制器方法: 我使用 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 Inside my migration: Profile model: Comment model: ProfilesController: And my form: 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 In You then change your migration to: and your controller method to: In your view (I used the
这篇关于如何捕获评论的作者,然后在工具提示中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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>
create
method in the controller I have: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
belongs_to :profile
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 'Add new' %>
<% end %>
User
(or whatever class current_user
returns) instead of the Profile
: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
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
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>