English 中文(简体)
如何抓住作者在工具上展示的评论?
原标题:How to capture a comment s author then display in tooltip?

我的表格附有可以提交简短评论的简介。 我想抓住提交人的名字,但这样,我就可以在把评论机构 h倒时在工具上展示。

www.un.org/Depts/DGACM/index_spanish.htm 在控制器中,我有:

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 %>

我正在考虑将作者与现用户联系起来。 或者是否有更简单、更好的办法?

<><>UPDATE: 如果能有更好的办法,我就得照亮这个名字。

最佳回答

你的解决方法是罚款,但我储存了<代码>。 用户(或每类<代码>现值_user/code> 回归)而不是<代码>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

之后,你将移民改为:

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

阁下认为(我使用<代码><> > <>代码>属性的确创造了一种工具,但感到可以自由使用你喜欢的任何方法:

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

I would suggest something like this:

在您的<代码>routes.rb中,为评论设立了一个专用资源。

resources :users do
  resources :comments
end

In your User model

class User
  has_many :comments
end

缩略语

class Comment
  belongs_to :user
end

。 方法

@comment = User.find(params[:user_id]).comments.new(params[:comment])

因此,该评论自动产生于该用户,而你不必通过任何东西。

Then, in your Comment view, you could just call its owners name

@comment.user.first_name




相关问题
VS 10 mangles html comments?

Using the latest VS 10 we created html markup, then commented it with html comments. The file on disk is not mangled, but when it renders, it renders the html comment tags, then removes some of the ...

Comments & OpenSource software [closed]

This may sound like a foolish question or an observation, but i have seen that most of the times when one tries to look at the opensource code, there are no comments or just one or two lines at the ...

Including commented Class declaration in implementation file

Everyone knows the advantages of a more readable code. So in order to make my code more readable what i do normally is include the commented class declaration in the implementation file of that class....

pair programming with comments [closed]

Over the years, I ve discovered that green-programmers tend to read the comments rather than the code to debug issues. Does having one person document the other person s code (and vice-versa) with ...

Commenting JavaScript functions á la Python Docstrings

It is valid JavaScript to write something like this: function example(x) { "Here is a short doc what I do."; // code of the function } The string actually does nothing. Is there any reason, ...

热门标签