I''m trying to validate a record before save. I''m using one controller in this case furnii where I have all my calls. The problem is that, the form containing the field I''m trying to validate resides in the furnii views. I tried to add validates_presence_of :content (the field name) in both the furnii model and the ratecomment model but it does not work. I know there are ways around this, suggestions anyone? #########controller code def rate @furnii = Furni.find(params[:id]) Rating.delete_all(["rateable_type = ''Furni'' AND rateable_id = ?", @furnii]) @furnii.add_rating Rating.new(:rating => params[:ratecomment]["rating"]) @comments = Ratecomment.new(params[:ratecomment]) if @comments.save redirect_to furni_path(@furnii) end 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sam, If you have class Ratecomment < ActiveRecord::Base validates_presence_of :some_field Then @comments.save should return false and the new instance of Ratecomment not retained if :some_field is blank, regardless of what controller it''s called in. Is that what you mean and what is not happening? On Aug 27, 12:56 pm, Sam Ginko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m trying to validate a record before save. I''m using one controller > in this case furnii where I have all my calls. The problem is that, the > form containing the field I''m trying to validate resides in the furnii > views. I tried to add validates_presence_of :content (the field name) in > both the furnii model and the ratecomment model but it does not work. I > know there are ways around this, suggestions anyone? > > #########controller code > def rate > @furnii = Furni.find(params[:id]) > Rating.delete_all(["rateable_type = ''Furni'' AND rateable_id = ?", > @furnii]) > @furnii.add_rating Rating.new(:rating => > params[:ratecomment]["rating"]) > @comments = Ratecomment.new(params[:ratecomment]) > if @comments.save > redirect_to furni_path(@furnii) > end > 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
smeade wrote:> Sam, > If you have > > class Ratecomment < ActiveRecord::Base > validates_presence_of :some_field > > Then @comments.save should return false and the new instance of > Ratecomment not retained if :some_field is blank, regardless of what > controller it''s called in. > > Is that what you mean and what is not happening?On Aug 27, 12:56�pm, Sam Ginko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> yes that is the issue. ########this is the model class Ratecomment < ActiveRecord::Base validates_presence_of :content end ##############this is the view <% form_tag( "rate?id=#{@furnii.id}", :name => "theform") do %> <table border="0" width="450" cellpadding="3" cellspacing="0"> <tr><td colspan=2><%= error_messages_for ''furni'' %></td></tr> <tr> <td class="txtBlackSmall" valign=top colspan=2> <table border="0" cellspacing="0" cellpadding="4"> <tr> <td class="headerBlackSmall">Rate it: </td> <td class="txtBlackSmall">1 <%= radio_button "ratecomment", "rating", "1", :checked => "checked"%></td> <td class="txtBlackSmall">2 <%= radio_button "ratecomment", "rating", "2" %></td> <td class="txtBlackSmall">3 <%= radio_button "ratecomment", "rating", "3" %></td> <td class="txtBlackSmall">4 <%= radio_button "ratecomment", "rating", "4" %></td> <td class="txtBlackSmall">5 <%= radio_button "ratecomment", "rating", "5" %></td> </tr> </table> </td> </tr> <tr> <td class="headerBlackSmall" colspan=2> <label for="content">Add comment</label><br> <%= text_area :ratecomment, :content, :rows=>"6", :cols=>"60" %><br> </td> </tr> <tr bgcolor="ffffff"> <td><%= link_to ''cancel'''''''''', index_furnii_path()%></td> <td align=right> <%= submit_tag "Submit" %> </td> </tr> </table> <%= hidden_field :ratecomment, :rateable_id, :value => @furnii.id %> <% 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
To clarify, is the Ratecomment getting created and saved or just the Rating is getting applied to the furnii? How do you know the Ratecomment is getting created, is it shown in another view or you see it in the database? On Aug 27, 2:23 pm, Sam Ginko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> smeade wrote: > > Sam, > > If you have > > > class Ratecomment < ActiveRecord::Base > > validates_presence_of :some_field > > > Then @comments.save should return false and the new instance of > > Ratecomment not retained if :some_field is blank, regardless of what > > controller it''s called in. > > > Is that what you mean and what is not happening? > > On Aug 27, 12:56 pm, Sam Ginko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > yes that is the issue. > > ########this is the model > class Ratecomment < ActiveRecord::Base > validates_presence_of :content > end > > ##############this is the view > > <% form_tag( "rate?id...-WAewTDFXyvZgoHlPtYpdqQ@public.gmane.org}", :name => "theform") do %> > <table border="0" width="450" cellpadding="3" cellspacing="0"> > > <tr><td colspan=2><%= error_messages_for ''furni'' %></td></tr> > <tr> > <td class="txtBlackSmall" valign=top colspan=2> > <table border="0" cellspacing="0" cellpadding="4"> > <tr> > <td class="headerBlackSmall">Rate it: </td> > <td class="txtBlackSmall">1 <%= radio_button "ratecomment", > "rating", "1", :checked => "checked"%></td> > <td class="txtBlackSmall">2 <%= radio_button "ratecomment", > "rating", "2" %></td> > <td class="txtBlackSmall">3 <%= radio_button "ratecomment", > "rating", "3" %></td> > <td class="txtBlackSmall">4 <%= radio_button "ratecomment", > "rating", "4" %></td> > <td class="txtBlackSmall">5 <%= radio_button "ratecomment", > "rating", "5" %></td> > </tr> > </table> > </td> > </tr> > <tr> > <td class="headerBlackSmall" colspan=2> > <label for="content">Add comment</label><br> > > <%= text_area :ratecomment, :content, > :rows=>"6", :cols=>"60" %><br> > </td> > </tr> > > <tr bgcolor="ffffff"> > <td><%= link_to ''cancel'''''''''', index_furnii_path()%></td> > <td align=right> > <%= submit_tag "Submit" %> > </td> > </tr> > </table> > > <%= hidden_field :ratecomment, :rateable_id, :value => @furnii.id %> > <% 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
smeade wrote:> To clarify, is the Ratecomment getting created and saved or just the > Rating is getting applied to the furnii? How do you know the > Ratecomment is getting created, is it shown in another view or you see > it in the database? >the Ratecomment gets created and saved in the database with a furnii_id. I''m redirecting it to the view in the furnii and it shows up no problem. the issue is if the user does not type anything in the text area field and submits. The validation some how is trying to look for a view. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The code does not appear to handle the case when @comments.save fails, so the default is to display the view for rate, which probably does not exist (it looks like rate is defined to handle only the posting of the form). It seems likely that you need something like this for the case when @comments.save fails: render :action=>rate_form On Aug 27, 2:05 pm, Sam Ginko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> smeade wrote: > > To clarify, is the Ratecomment getting created and saved or just the > > Rating is getting applied to the furnii? How do you know the > > Ratecomment is getting created, is it shown in another view or you see > > it in the database? > > the Ratecomment gets created and saved in the database with a furnii_id. > I''m redirecting it to the view in the furnii and it shows up no problem. > the issue is if the user does not type anything in the text area field > and submits. The validation some how is trying to look for a view. > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---