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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 19, 8:39 pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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> > Am I missing smething vital > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #993841:> On Apr 19, 8:39pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Tim Shaffer wrote in post #993815: >> > > sounds to me like params[:id] isn''t set. > > FredNot 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 %> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 Apr 2011, at 23:47, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #993841: >> On Apr 19, 8:39pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> 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> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>> >> <%= 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 20, 10:40 am, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> >> <%= 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> 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 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> have you tried form_for [@comment, :subcomment] ? > This will make sure that the http method etc is right for creating a > new comment > > FredI 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 20, 11:44 am, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > 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> > Confused > > Neil > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #993985:> On Apr 20, 11:44am, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > FredBack 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 20, 12:46 pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > 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> <% end %> > </div> > </div> > <% end %> > > How do I ''make sure that @comment is set to the comment to which > subcomments should be added to'' ?> > Neil > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #994021:> On Apr 20, 12:46pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 20, 2:49 pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #994021:> On Apr 20, 12:46pm, Neil Bye <li...@ruby-forum.com> 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 > IDCheck the parameters you get in your controller - you''ll probably need to use params[:comment_id] rather than params[:id] Fred> > Neil > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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] > > FredTried 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 20, 3:33 pm, Neil Bye <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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> > Neil > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Apr 19, 2011 at 1:52 PM, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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]) > -I4CJOCdi0N4XD9ON9pCQiXPdqI+L0LjizdLgt8pqfDw@public.gmane.org( :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> 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. > > ????????????????????? > > NeilI 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.