Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Using current_user as author in new post
5 Posts
Using current_user as author in new post

Greetings,

That's great,. i could have done this for days if it's the other way.. thanks again!.

The #merge method is a simple way to combine two hashes. It will apply the parameter hash to the hash receiving the message, which means if both hashes have the same key the result will associate that key with the value from the parameter hash. To be more clear:

hash1 = { :a => 1, :b => 2 }
hash2 = { :a => 99, :c => 3 }
hash1.merge(hash2) # { :a => 99, :b => 2, :c => 3 }
hash2.merge(hash1) # { :a => 1, :b => 2, :c => 3 }

You could also do this in your controller:

@comment = @post.comments.new(params[:comment])
@comment.author = current_user
if @comment.save
  ...

I prefer using #merge because it keeps all of the Comment creation in one statement. It also saves one line of code, although it does make that line more complex. Neither way is wrong.

Greetings,

Terrific!, that's exactly what i need!, am sorry for my poor english :D. you saved my day, if only you knew, was working on this for days already, and am still studying rails, i even bought a book on amazon haha,. thank you very much, more power to you ^_^

edit: i want to know, why did we use .merge method to do that?, is there any other way than that? sorry, just asking.. thank you again.

I don't understand what you mean when you say you're using a session controller. But, if I understand your question correctly you're trying to associate the logged in user with each command he or she submits, as well as display the associated user on the show page for comments. Is that correct?

I'll assume you have a User model, and #current_user is a method you've made available to your controllers. Given that, you can create the association you want easily in your Comments#create action:

@comment = @post.comments.new(params[:comment].merge(:author => current_user)

Displaying the associated user in the Comments#show view is as simple as adding a field:

Author: 

Hopefully that's helpful. Also, a couple other things:

  1. The #create! method for creating a new ActiveRecord will throw an exception if the new object is not valid. So this code:
  @comment = @post.comments.create!(params[:comment])
  if @comment = @post.comments.create!(params[:comment])

will never fail the conditional; it will succeed or raise an exception. You're also creating the object twice, which I assume is a typo. Finally, you should probably handle the case where the author creates a bad comment, such as submitting an empty body. I would suggest something like this:

@comment = @post.comments.new(params[:comment].merge(:author => current_user)
if @comment.save
  redirect_to @post
else
  render :action => :new
end

Greetings,

Am new to rails, and like 2 months i am studying this. i was doing a simple blog, where i want the current user name to appear on his post using sessions. i have a form, that has a body, and a post_id. and another session controller.

i was working with this on my comments controller

def create

@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])
if @comment = @post.comments.create!(params[:comment])
  respond_to do |format|
  format.html { redirect_to @post }
  format.js
  end
end

end

this on my comment views

<br />

<br />

and another method for the current user using sessions. i hope am making a sense here.. if anybody knows how to do this, pls help me. thank you very much in advance..

5 Posts
Login to add your message