Partials and comments and other fun things!


#1

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?


#2

On Thu, Jan 8, 2009 at 3:26 PM, Roger M.
removed_email_address@domain.invalid 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?


#3

Franz S. wrote:

On Thu, Jan 8, 2009 at 3:26 PM, Roger M.
removed_email_address@domain.invalid 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


#4

On Thu, Jan 8, 2009 at 4:16 PM, Roger M.
removed_email_address@domain.invalid 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


#5

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:

http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial

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?


#6

Franz S. wrote:

On Thu, Jan 8, 2009 at 6:59 PM, Roger M.
removed_email_address@domain.invalid 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


#7

On Fri, Jan 9, 2009 at 3:37 PM, Roger M.
removed_email_address@domain.invalid 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


#8

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.


#9

On Thu, Jan 8, 2009 at 6:59 PM, Roger M.
removed_email_address@domain.invalid 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


#10

On Fri, Jan 9, 2009 at 4:03 PM, Roger M.
removed_email_address@domain.invalid 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| -%>