Partials and comments and other fun things!

Hey folks, first timer here so be gentle. I have a messages page which
uses a partial to display comments for each message for an article and a
little box under each message so people can comment on that message.
located at http://localhost:3000/articles/6/messages


partial code
<%= render :partial => “comment”, :object => message %>


show messages code (messages controller)
def show
@message = @article.messages.find(params[:id])
@message_comments = @message.comments

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @message }
end

end

post comment code (found in messages controller)

def post_comment
@message = @article.messages.find(params[:id])
@comment = Comment.new(
“message_id” => @message.id,
“created_at” => Time.now,
“body” => params[:comment][‘comment’],
“author” => @current_user.first_name
)
if @comment.save
flash[:notice] = ‘Comment was successfully added.’
redirect_to(article_messages_path(@article))
end
end


_messages partial code

<% form_tag :action => ‘post_comment’ do %>

Comment
<%= text_field 'comment', 'comment' %>

<%= submit_tag "Post" %>

<% end %>

the page loads but when I click post to post the comment it does not put
any data in the database and returns the following error…!

ActiveRecord::RecordNotFound in MessagesController#post_comment

Couldn’t find Article without an ID

it seems to be coming from the before filter when the page is refreshed
after posting.

def get_article
@article = Article.find(params[:article_id])
end

used for the routes… can anyone help address this issue?

On Thu, Jan 8, 2009 at 3:26 PM, Roger M.
[email protected] wrote:

Hey folks, first timer here so be gentle. I have a messages page which
uses a partial to display comments for each message for an article and a
little box under each message so people can comment on that message.
located at http://localhost:3000/articles/6/messages

Can you post the relevant routes you have?

Franz S. wrote:

On Thu, Jan 8, 2009 at 3:26 PM, Roger M.
[email protected] wrote:

Hey folks, first timer here so be gentle. I have a messages page which
uses a partial to display comments for each message for an article and a
little box under each message so people can comment on that message.
located at http://localhost:3000/articles/6/messages

Can you post the relevant routes you have?

map.resources :articles do |articles|
articles.resources :messages
end

On Thu, Jan 8, 2009 at 4:16 PM, Roger M.
[email protected] wrote:

map.resources :articles do |articles|
articles.resources :messages
end

and comments belong_to :messages right? if so, add it in the routes

map.resources :articles do |articles|
articles.resources :messages do |messages|
messages.resources :comments
end
end

It is better practice to have a CommentsController to operate on your
comments instead
of using the Messages controller. I highly suggest that you then use
this tutorial as your
starting point to move forward:

Good luck.

Franz

It is better practice to have a CommentsController to operate on your
comments instead
of using the Messages controller. I highly suggest that you then use
this tutorial as your
starting point to move forward:

Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com

Good luck.

Franz

Hi Franz,

Thanks, ive got a comments controller being used now, along with a
partial link,

<% for message in @messages %>
<%= render :partial => @comment = Comment.new, :locals => { :button_name
=> ‘Post’}%>
<% end %>

this creates commments but without a message_id

so I need to somehow pass the message_id to the partial so that it goes
into the database.

any ideas?

Franz S. wrote:

On Thu, Jan 8, 2009 at 6:59 PM, Roger M.
[email protected] wrote:

<% for message in @messages %>
<%= render :partial => @comment = Comment.new, :locals => { :button_name
=> ‘Post’}%>
<% end %>

If you have your association setup properly between Message and Comment
models,
then you can do

@comment = message.comments.new

Franz

hey franz, sorry to be a pain, but I have tried that, and the
relationships are in my models already and it still is not putting the
id value in the database.

message has_many :comments
comment belongs_to :message

On Fri, Jan 9, 2009 at 3:37 PM, Roger M.
[email protected] wrote:

relationships are in my models already and it still is not putting the
id value in the database.

message has_many :comments
comment belongs_to :message

Can you try this in your console?

m = Message.find(:first)
c = m.comments.new

That should automatically set c.message_id to m.id to make the
association.

Franz

Can you try this in your console?

m = Message.find(:first)
c = m.comments.new

That should automatically set c.message_id to m.id to make the
association.

Franz

yep that works spot on.

On Thu, Jan 8, 2009 at 6:59 PM, Roger M.
[email protected] wrote:

<% for message in @messages %>
<%= render :partial => @comment = Comment.new, :locals => { :button_name
=> ‘Post’}%>
<% end %>

If you have your association setup properly between Message and Comment
models,
then you can do

@comment = message.comments.new

Franz

On Fri, Jan 9, 2009 at 4:03 PM, Roger M.
[email protected] wrote:

Franz

yep that works spot on.

OK, so the associations are in place.

Can you show the form you use for creating/editing comments? Assuming
that in your
CommentsController, you have a before_filter to get the @message à la:

@message = Message.find(params[:message_id])

and in your new method: @comment = @message.comments.new
while in your edit method: @comment =
@message.comments.find(params[:id])

You have to make sure that in your form, you do

<% form_for([@message, @comment]) do |f| -%>

<% end -%>

and of course if you nested Message under Article, it would be

<% form_for([@message.article, @message, @comment]) do |f| -%>