Forum: Ruby on Rails partials and comments and other fun things!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-01-08 15:26
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 %>
  <p><label for="comment_comment">Comment</label><br/>
      <%= text_field 'comment', 'comment'  %>
  </p>

    <%= 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?
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2009-01-08 16:14
(Received via mailing list)
On Thu, Jan 8, 2009 at 3:26 PM, Roger Muthton
<rails-mailing-list@andreas-s.net> 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?
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-01-08 16:16
Franz Strebel wrote:
> On Thu, Jan 8, 2009 at 3:26 PM, Roger Muthton
> <rails-mailing-list@andreas-s.net> 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
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2009-01-08 16:59
(Received via mailing list)
On Thu, Jan 8, 2009 at 4:16 PM, Roger Muthton
<rails-mailing-list@andreas-s.net> 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:

http://www.akitaonrails.com/2007/12/12/rolling-wit...

Good luck.

Franz
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-01-08 18:59
>
> 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-wit...
>
> 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?
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2009-01-09 09:12
(Received via mailing list)
On Thu, Jan 8, 2009 at 6:59 PM, Roger Muthton
<rails-mailing-list@andreas-s.net> 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
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-01-09 15:37
Franz Strebel wrote:
> On Thu, Jan 8, 2009 at 6:59 PM, Roger Muthton
> <rails-mailing-list@andreas-s.net> 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
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2009-01-09 15:43
(Received via mailing list)
On Fri, Jan 9, 2009 at 3:37 PM, Roger Muthton
<rails-mailing-list@andreas-s.net> 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
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-01-09 16:03
>
> 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.
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2009-01-10 13:49
(Received via mailing list)
On Fri, Jan 9, 2009 at 4:03 PM, Roger Muthton
<rails-mailing-list@andreas-s.net> 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| -%>
  <!-- form body -->
<% end -%>

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

<% form_for([@message.article, @message, @comment]) do |f| -%>
This topic is locked and can not be replied to.