ruby on rails 3 - How to capture a comment's author then display in tooltip? -


I have a form attached to the profile, where concise comments can be submitted. I want to capture the name of the author, though I can display it in the tooltip when the comment is hovering over the body.

I have my create method in my controller: def make @ comment = comment.New (consultation []: ) @ commented out! Redirect_to profile_path (@ comment.profile) end

inside my migration:

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

profile model:

  related_to: user accepts_nested_attributes_for: user has_many: comments   

comment models : / P>

  related_to: profile   

profile controller:

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

and my form:

  & lt; % = Form_for @comment do F | & Gt%; & Lt;% = f.hidden_field: profile_id ,: value = & gt; @ Profile id = & gt; & Lt;% = f.hidden_field: author_id ,: value = & gt; "# {Current_user.profile.first_name} # {current_user.profile.last_name}"%> & Lt;% = f.text_field: body% & gt; & Lt;% = f.submit 'Add New'% & gt; & Lt;% end% & gt;   

I was thinking of using that support to add and display the author_id to current_user.profile.id: first_name and: last_name which are the properties of the profile.

Update: I have got to show this name, though I am still curious if there is a better way.

Your solution looks ok, but I user (or whatever class current_user profile :

in app / model / comment .rb :

  Returns)> class comment & lt; ActiveRecord :: Base is_to: Profile belongs_to: author ,: class_name = & gt; "User",: foreign_key = & gt; "Author_id" ... code of the rest ... end   

Then you can change your migration here:

  t.integer: author_id   

and for your controller method:

  def can create @comment = comment.New (params [: comment]. Major (: author_id = & gt; ; Current_user.id)) @ Comment.save! Redirect_to profile_path (@ comment.profile) end   

Use tooltip in your view (I used the title attribute, but use the method you like Do not hesitate to):

  & lt; Div class = "comment" title = "& lt;% = @ comment.author.profile.first_name%> & lt;% = @ comment.author.profile.last_name% & gt;" & gt; & Lt;% = @ comment.body% & gt; & Lt; / Div & gt;    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -