Topic: Creating a weblog in 15 minutes with Rails 2 - help understanding code

Hello. I have been following a ton of different tutorials to learn ruby on rails and create my blog app. I recently added a comment model to my app following the screencast "[link]Creating a weblog in 15 minutes with Rails 2[/link]".

Adding a comment works. However, I can not get it to save the current users id to the comment row. I think this is due to me not correctly understanding the code in the tutorial. Please help me understand the code.

I have a helper method "current_user" which returns the users id.

Comment model- :post_id, :body, :user_id

The controller code from the tutorial, with the addition of my one line to save the current users id to the "user_id" column (which does not work).

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create!(params[:comment]) #! I do not understand this line
    @comment.user_id = current_user.id # Add current user_id to comment
    respond_to do |format|
      format.html { redirect_to @post }
      format.js
    end
  end
 end

I also made note of the line of code that I do not understand. Thank you guys. You have already helped out a bit. hopefully I will be able to contribute back one day!

Learning rails, one day at a time.

http://keilmiller.com

Re: Creating a weblog in 15 minutes with Rails 2 - help understanding code

Okay. Doing some more searching, I came across [linkto=http://railsforum.com/viewtopic.php?pid=83644#p83644]this[/link] on another forum.

I modified my code to this and it worked.

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(params[:comment])
    @comment.user_id = current_user.id # Add current user_id to comment
    @comment.save
    respond_to do |format|
      format.html { redirect_to @post }
      format.js
    end
  end
 end

I used "build" and "save" instead of "create".  Usually, to create a database entry I use "new" for example;

@post = Post.new(params[:post])

Can some one explain the differences between build, create and new? Placing validations does not show the error message too.

Last edited by tuscanidream (2010-02-24 00:39:17)

Learning rails, one day at a time.

http://keilmiller.com

Re: Creating a weblog in 15 minutes with Rails 2 - help understanding code

The reason your first sample failed is that you never saved the comment object after you assigned it the user_id.

For a treatment of build vs new vs create, see http://vinhboy.com/blog/2009/01/15/rails-new-vs-build/

Re: Creating a weblog in 15 minutes with Rails 2 - help understanding code

Thanks guys. Any tips on adding validations (presense_of) to the comment, and destroying the comment?

I tried adding validations to the model, and they worked. However, flash_error and fieldWithErrors didn't work.

Learning rails, one day at a time.

http://keilmiller.com