Couldn't find Comment without an ID

Im trying to makee an application that allows subcomments ie comments on
comments. The point where I get stuck is finding the comment that is
commented on.

This is an excerpt from subcomment.rb

def create
    @comment = Comment.find(params[:id])
    @comment.subcomments.create( :comment_id => @comment.id , :body =>
params[:subcomment][:body])
    render :update do |page|
      page.reload
    end
end

The second line is obviously wrong (and bviously works if I give a value
for :id) so I think I might have the wrong approach.

Can anyone shed light on this issue.

What does your model look like? Do you have the appropriate has_many and belongs_to for the “subcomments” to work?

In general, you shouldn’t need to specify comment_id for the child when you access it via the has_many relationship.

For example:

@comment = Comment.find(params[:id])
new_comment = @comment.subcomments.create

Rails knows that new_comment is a child of @comment since you accessed it via subcomments.

Another example:

@comment = Comment.find(params[:id])
new_comment = Comment.create(:comment_id=>@comment.id)

This is basically the same as above, except instead of creatin through subcomments, you can specify the parent comment_id.

Tim Shaffer wrote in post #993815:

What does your model look like? Do you have the appropriate has_many and
belongs_to for the "subcomments" to work?

In general, you shouldn't need to specify comment_id for the child when
you
access it via the has_many relationship.

@comment = Comment.find(params[:id])
new_comment = Comment.create(:comment_id=>@comment.id)

This is basically the same as above, except instead of creatin through
subcomments, you can specify the parent comment_id.

In comment.rb I have has_many :subcomments
In subcomment.rb I have belongs_to comment

Is that correct.

Both of your examples throw up 'Couldn't find Comment without an ID'

Am I missing smething vital

Tim Shaffer wrote in post #993815:

Is that correct.

Both of your examples throw up 'Couldn't find Comment without an ID'

sounds to me like params[:id] isn't set.

Fred

Frederick Cheung wrote in post #993841:

Tim Shaffer wrote in post #993815:

sounds to me like params[:id] isn't set.

Fred

Not sure what you mean. How would I set params[:id] ?

It works if I use params[:user_id] or [:current_user_id]

Surely the pproblem is that it doesn't know which comment it is supposed
to be subcommenting on.

This is what calls it, is there a problem here.

<%= form_for :subcomment, :remote => true, :url =>
user_subcomments_path(@user) do |form| %>
           <%= form.text_field :body %>
         <p><%= submit_tag 'Comment' %></p>
         <% end %>

Frederick Cheung wrote in post #993841:

Tim Shaffer wrote in post #993815:

sounds to me like params[:id] isn't set.

Fred

Not sure what you mean. How would I set params[:id] ?

Typically it will come from the form or the URL, but if you try and use it when it's not set, things will blow up.

It works if I use params[:user_id] or [:current_user_id]

Surely the pproblem is that it doesn't know which comment it is supposed
to be subcommenting on.

That us precisely it - you're trying to create the subcomment on the comment fetched by params[:id], but params[:id] isn't set

This is what calls it, is there a problem here.

<%= form_for :subcomment, :remote => true, :url =>
user_subcomments_path(@user) do |form| %>
          <%= form.text_field :body %>
        <p><%= submit_tag 'Comment' %></p>
        <% end %>

Yes - nothing in this form says which comment the newly created subcomment belongs to. In general you can either use a hidden_field with the id or make it part of the URL, eg as a nested resource (in which case params[:comment_id] would contain the correct value).
I would normally have
comment_subcomments_path(@comment) (assuming @comment is what the user wants to comment on) rather than user_subcomments_path(@user) with a hidden field with the comment_id because the user creating an object is usually obtainable via your login system (indeed in most cases you don't want people to be able to create subcomments as other users just by editing the URL the form posts to)

Fred

<%= form_for :subcomment, :remote => true, :url =>
user_subcomments_path(@user) do |form| %>
          <%= form.text_field :body %>
        <p><%= submit_tag 'Comment' %></p>
        <% end %>

comment_subcomments_path(@comment) (assuming @comment is what the user
wants to comment on) rather than user_subcomments_path(@user) with a
hidden field with the comment_id because the user creating an object is
usually obtainable via your login system (indeed in most cases you don't
want people to be able to create subcomments as other users just by
editing the URL the form posts to)

Ok I see your point but now I run into a routes problem which doesn't
come up if I use user_subcomments_path(@user)

When I use 'comment_subcomments_path(@comment)' I get 'No route matches
{:controller=>"subcomments"}'

Of course subcomments_controller exists

This is from routes.rb

resources :comments
resources :subcomments
resources :users

resources :users do

        resources :comments
  end

  resources :comments do

        resources :subcomments
  end

  resources :users do

        resources :subcomments
  end

>> <%= form_for :subcomment, :remote => true, :url =>
>> user_subcomments_path(@user) do |form| %>
>> <%= form.text_field :body %>
>> <p><%= submit_tag 'Comment' %></p>
>> <% end %>
> comment_subcomments_path(@comment) (assuming @comment is what the user
> wants to comment on) rather than user_subcomments_path(@user) with a
> hidden field with the comment_id because the user creating an object is
> usually obtainable via your login system (indeed in most cases you don't
> want people to be able to create subcomments as other users just by
> editing the URL the form posts to)

Ok I see your point but now I run into a routes problem which doesn't
come up if I use user_subcomments_path(@user)

When I use 'comment_subcomments_path(@comment)' I get 'No route matches
{:controller=>"subcomments"}'

have you tried form_for [@comment, :subcomment] ?
This will make sure that the http method etc is right for creating a
new comment

Fred

have you tried form_for [@comment, :subcomment] ?
This will make sure that the http method etc is right for creating a
new comment

Fred

I now have:

<%= form_for [@comment, :subcomment], :remote => true, :url =>
comment_subcomments_path(@comment) do |form| %>
           <%= form.text_field :body %>
<% end %>

But sill get the No route matches {:controller=>"subcomments"}

Confused

Neil

> have you tried form_for [@comment, :subcomment] ?
> This will make sure that the http method etc is right for creating a
> new comment

> Fred

I now have:

<%= form_for [@comment, :subcomment], :remote => true, :url =>
comment_subcomments_path(@comment) do |form| %>
<%= form.text_field :body %>
<% end %>

But sill get the No route matches {:controller=>"subcomments"}

Sorry, that was wrong - it should be form_for [@comment,
Subcomment.new], you don't need the :url option
You should also make sure that @comment is set to the comment to which
subcomments should be added to

Fred

Frederick Cheung wrote in post #993985:

     <%= form.text_field :body %>
<% end %>

But sill get the No route matches {:controller=>"subcomments"}

Sorry, that was wrong - it should be form_for [@comment,
Subcomment.new], you don't need the :url option
You should also make sure that @comment is set to the comment to which
subcomments should be added to

Fred

Back to

Started POST "/subcomments" for 127.0.0.1 at 2011-04-20 12:36:11 +0100
  Processing by SubcommentsController#create as JS
  Parameters: {"utf8"=>"✓",
"authenticity_token"=>"mqjMuo0L9PDPZpLdoB5z2AO93wdub+J4A+hXycWxg6U=",
"subcomment"=>{"body"=>"will this work"}, "commit"=>"Create Subcomment"}
  User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" =
6 LIMIT 1
Completed in 142ms

ActiveRecord::RecordNotFound (Couldn't find Comment without an ID):

My subcomments_controller.rb has:

def create
    @comment = Comment.find(params[:id])
    @comment.subcomments.create( :comment_id => @comment.id , :body =>
params[:subcomment][:body])
    render :update do |page|
      page.reload
    end
  end

_comment.html.erb has

<% @user.comments.each do |comment| %>
<div id="remark"<p><%= comment.body %></p>
<div id="commenter"<p><%= comment.story_id %>
<%= form_for [@comment, Subcomment.new] do |form| %>
           <%= form.text_field :body %>
           <p><%= form.submit %></p>
<% end %>
</div>
</div>
<% end %>

How do I 'make sure that @comment is set to the comment to which
subcomments should be added to' ?

Neil

def create
@comment = Comment.find(params[:id])
@comment.subcomments.create( :comment_id => @comment.id , :body =>
params[:subcomment][:body])
render :update do |page|
page.reload
end
end

_comment.html.erb has

<% @user.comments.each do |comment| %>
<div id="remark"<p><%= comment.body %></p>
<div id="commenter"<p><%= comment.story_id %>
<%= form_for [@comment, Subcomment.new] do |form| %>
<%= form.text_field :body %>
<p><%= form.submit %></p>

Ah, so since comment is clearly the comment for which you want create
subcomments, that should be form_for [comment, Subcomment.new] - I had
got the impression that this was on a single 'show' page for a
particular comment (where @comment would usually be the comment being
shown)

Fred

Frederick Cheung wrote in post #994021:

>> _comment.html.erb has

>> <, that should be form_for [comment, Subcomment.new] - I had

I got there on that but it still says Couldn't find Comment without an
ID

Check the parameters you get in your controller - you'll probably need
to use params[:comment_id] rather than params[:id]

Fred

Frederick Cheung wrote in post #994032:

Check the parameters you get in your controller - you'll probably need
to use params[:comment_id] rather than params[:id]

Fred

Tried params[:comment_id]

It goes to /comments/1/subcomments and says:

try {
window.location.reload();
} catch (e) { alert('RJS error:\n\n' + e.toString());
alert('window.location.reload();'); throw e }

Does this mean its gone through and found another error.

???

Neil

Frederick Cheung wrote in post #994032:

> Check the parameters you get in your controller - you'll probably need
> to use params[:comment_id] rather than params[:id]

> Fred

Tried params[:comment_id]

It goes to /comments/1/subcomments and says:

try {
window.location.reload();} catch (e) { alert('RJS error:\n\n' + e.toString());

alert('window.location.reload();'); throw e }

Does this mean its gone through and found another error.

???

That means that it rendered your rjs (which is telling the page to
reload) but the form wasn't setup to to receive a rjs response. Your
subcomment should have been created though.

Fred

Im trying to makee an application that allows subcomments ie comments on
comments. The point where I get stuck is finding the comment that is
commented on.

This is an excerpt from subcomment.rb

def create
@comment = Comment.find(params[:id])
@comment.subcomments.create( :comment_id => @comment.id , :body =>
params[:subcomment][:body])
render :update do |page|
page.reload
end
end

Is there any reason you're using create instead of build? This is how
I would code it.

@comment.subcomments.build(params[:subcomment])

Note: I haven't tested it, etc. etc.

Trevor Oke

Neil Bye wrote in post #994040:

Frederick Cheung wrote in post #994032:

Check the parameters you get in your controller - you'll probably need
to use params[:comment_id] rather than params[:id]

Fred

Tried params[:comment_id]

It goes to /comments/1/subcomments and says:

try {
window.location.reload();
} catch (e) { alert('RJS error:\n\n' + e.toString());
alert('window.location.reload();'); throw e }

Does this mean its gone through and found another error.

???

Neil

I was right it did go through. The mystery js must have come from

render :update do |page|
      page.reload
    end
Is that because in the comment creator I used form_for Remote = true ?

Anyway if I relpace the above with a simple redirect it works.

Thanks for you patient help.

Neil