Code reviews: my dumb use of acts_as_commentable (newbie)

Most of your questions will be answered by simply changing the way you are saving comments. You need the review's id in a hidden field so that you can add the comment to the correct review, but try this:

@review = Review.find(params[:review_id]
@new_comment = @review.create_in_comments(params[:comment])

Change your comment fields to be named "comment[title]" and "comment[comment]" so that you can just pass params[:comment]. The id and type will be saved automatically. Since @new_comment will contain the new comment, there is no need for the extra methods. Let me know if you have more questions.

Tom Harrison wrote:

BTW, one good way to get an idea of the methods that any relationship
add to a model is to use script/console and look at the methods of an
instance of said model. For your review example, try this:

`r = Review.find(:first)

puts r.methods.sort.join("\n")

puts r.methods.grep(/comment/).sort.join("\n")`

The first call to methods will list all of the public methods available
to an instance of your Review class. The second will just show any
methods that have “comment” in their name. Then you can simply play
around with them in console or search a given api for more info on a
method. Hope I was of some help.


William Pratt wrote:

One other thing I noticed. You should just use form_tag instead of
form_for :review. form_for is meant to create a form around a specific
object, and since your comment is not a review, form_tag would make
more sense.


<% form_tag :action => “save_comment” do %>

Add Your Own Comment

<%= hidden_field_tag :review_id %>


<%= text_field_tag “comment[title]”, :size => 40, :id =>
‘title’ %>


<%= text_field_tag "comment[comment], :size => 200, :id =>
‘comment’ %>

<%= submit_tag “Save Comment”, :class => “submit” %>

<% end %>

`This should give you the jist of it. I have not tested this code,
but it should at least get you started.

William Pratt wrote:

Although, you could also use form_for :comment. In that case, you can
just name your fields :title, and :comment. Either way is fine. Note
that I added id’s to the tags because the default is to use the name as
the id and []'s are not allowed in id’s, only alphanumerics and _'s.

William Pratt wrote:

Ahh, don’t be so hard on yourself, everyone has been where you are. I
enjoy helping on this list and so do many others. This list truly has a
good group of people. Have fun and let me know if I can help with
anything else.

William Pratt wrote:

Can we use acts_as_commentable for different models in same app?

yes. i don't see why not.

MaD wrote:

Can we use acts_as_commentable for different models in same app?

yes. i don't see why not.

Thanks for replying.

Do you have some sample code?

I am bit confused,we are using comments for say blog and note in one app
so are they going to refer same table comment or what?