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.
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)
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"}'
>> <%= 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
> 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
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' ?
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)
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.
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.