Can you post the relevant routes you have?
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?
If you have your association setup properly between Message and Comment models, then you can do
@comment = message.comments.new
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
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.
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| -%> <!-- form body --> <% end -%>
and of course if you nested Message under Article, it would be
<% form_for([@message.article, @message, @comment]) do |f| -%>