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 obviously 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 S. 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 something vital
On Apr 19, 8:39pm, Neil B. [email protected] wrote:
Tim S. 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 C. wrote in post #993841:
On Apr 19, 8:39pm, Neil B. [email protected] wrote:
Tim S. 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 %>
<%= submit_tag ‘Comment’ %>
<% end %>
<%= form_for :subcomment, :remote => true, :url =>
user_subcomments_path(@user) do |form| %>
<%= form.text_field :body %>
<%= submit_tag ‘Comment’ %>
<% 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
On Apr 20, 10:40am, Neil B. [email protected] wrote:
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
On 19 Apr 2011, at 23:47, Neil B. [email protected] wrote:
Frederick C. wrote in post #993841:
On Apr 19, 8:39pm, Neil B. [email protected] wrote:
Tim S. 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 %>
<%= submit_tag ‘Comment’ %>
<% 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
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
On Apr 20, 11:44am, Neil B. [email protected] wrote:
<%= 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 C. wrote in post #993985:
On Apr 20, 11:44am, Neil B. [email protected]n.invalid wrote:
<%= 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"
<%= comment.body %>
<div id="commenter"
<%= comment.story_id %>
<%= form_for [@comment, Subcomment.new] do |form| %>
<%= form.text_field :body %>
<%= form.submit %>
<% end %>
<% end %>
How do I ‘make sure that @comment is set to the comment to which
subcomments should be added to’ ?
Neil
On Apr 20, 12:46pm, Neil B. [email protected] wrote:
_comment.html.erb has
<% @user.comments.each do |comment| %>
<div id="remark"<%= comment.body %>
<div id="commenter"<%= comment.story_id %>
<%= form_for [@comment, Subcomment.new] do |form| %>
<%= form.text_field :body %>
<%= form.submit %>
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
<% end %>
<% end %>
How do I ‘make sure that @comment is set to the comment to which
subcomments should be added to’ ?
On Apr 20, 2:49pm, Neil B. [email protected] wrote:
Frederick C. wrote in post #994021:> On Apr 20, 12:46pm, Neil B.
[email protected] wrote:
_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 C. wrote in post #994021:
On Apr 20, 12:46pm, Neil B. [email protected] wrote:
_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
Neil
Frederick C. 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
On Apr 20, 3:33pm, Neil B. [email protected] wrote:
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
On Tue, Apr 19, 2011 at 1:52 PM, Neil B. [email protected] wrote:
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 B. wrote in post #994040:
Frederick C. 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