hi, in my controller i am returning an instance of my model to a form. this form uses a partial for contact information. the fields in the form are also part on my model. but when i add validates_presence_of :name to my model, the error message does not appear when i submit the form with the name field blank. could this be a problem with my partial? or in my db, must the default values for fields be null? i put an if check that checks if the field is empty and that returns true. -- 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 -~----------~----~----~----~------~----~------~--~---
Jimmy Kittiyachavalit
2006-Dec-02 19:22 UTC
Re: validates_presence_of not working...could it be my partial?
Could you post the relevant code? On 12/2/06, koloa <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > hi, > in my controller i am returning an instance of my model to a form. this > form uses a partial for contact information. the fields in the form are > also part on my model. > > but when i add validates_presence_of :name to my model, the error > message does not appear when i submit the form with the name field > blank. > > could this be a problem with my partial? or in my db, must the default > values for fields be null? i put an if check that checks if the field is > empty and that returns true. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
mycontr... def form @cat = getMainCatName(params[:fid]) getForm(@cat) end application.rb... def getForm(category) case category when "re" @temp = House.new @ddvd_cover = Repicture.new @cat = ''re'' when "job" @temp = Job.new @cat = ''job'' end render :template => "displays/form" end form.rhtml <% if @cat == "job"%> <%= start_form_tag :action => ''create'', :category => ''job''%> <%= render :partial => ''displays/fjob'' %> <%= submit_tag "Create"%> <%= end_form_tag%> <%end%> _fjob.rhtml <%= error_messages_for ''temp'' %> <!--[form:temp]--> <p><label for="jobs_company">Company</label><br/> <%= text_field :temp, ''company'' %></p> <p><label for="jobs_description">Description</label><br/> <%= text_area :temp, ''description'', :cols => "30", :rows=> "10" %></p> ...etc... <!--[eoform:temp]--> modelJob class Job < ActiveRecord::Base validates_presence_of : company, : description, :on => :create, :message => "can''t be blank" end thanks for any help, been kind of stuck on this all day. -- 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 -~----------~----~----~----~------~----~------~--~---
ive been reading and it seems that my problem may be the redirect? anyway around this? -- 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 -~----------~----~----~----~------~----~------~--~---
do i need any modifications to my css file? -- 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 -~----------~----~----~----~------~----~------~--~---
Chris T
2006-Dec-03 14:27 UTC
Re: validates_presence_of not working...could it be my parti
koloa wrote:> > mycontr... > > def form > @cat = getMainCatName(params[:fid]) > getForm(@cat) > end > > application.rb... > > def getForm(category) > case category > when "re" > @temp = House.new > @ddvd_cover = Repicture.new > @cat = ''re'' > when "job" > @temp = Job.new > @cat = ''job'' > end > render :template => "displays/form" > end > > > > form.rhtml > <% if @cat == "job"%> > <%= start_form_tag :action => ''create'', :category => ''job''%> > <%= render :partial => ''displays/fjob'' %> > <%= submit_tag "Create"%> > <%= end_form_tag%> > <%end%> > > > _fjob.rhtml > > <%= error_messages_for ''temp'' %> > <!--[form:temp]--> > > <p><label for="jobs_company">Company</label><br/> > <%= text_field :temp, ''company'' %></p> > > <p><label for="jobs_description">Description</label><br/> > <%= text_area :temp, ''description'', :cols => "30", :rows=> "10" %></p> > ...etc... > <!--[eoform:temp]--> > > > modelJob > class Job < ActiveRecord::Base > validates_presence_of : company, : description, :on => :create, > :message => "can''t be blank" > end > > > thanks for any help, been kind of stuck on this all day. > > > >What happens if you try to create an instance of Jon in the console. Try doing this (in the console): j = Job.new j.save p j.errors You should see whether the validation is working -- as j.errors will have a bundle of errors (not sure, by the way that it is allowed to have a space between the colon and the rest of the symbol -- would have thought you wanted validates_presence_of :company, :description HTH --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hi chris thanks for the suggestion. i just tried it and it seems that the error condition is coming out correct. console reports that my validates error flags are raised when my text field is nil. so it must have to do with my redirect? -- 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 -~----------~----~----~----~------~----~------~--~---
Chris T
2006-Dec-03 17:34 UTC
Re: validates_presence_of not working...could it be my parti
koloa wrote:> hi chris thanks for the suggestion. i just tried it and it seems that > the error condition is coming out correct. console reports that my > validates error flags are raised when my text field is nil. > > so it must have to do with my redirect? > > >Don''t know -- don''t think you posted the create action, which is I guess where this was... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hi def create @job = Job.new(params[:temp]) if @job.save flash[:notice] = ''Profile was successfully created.'' redirect_to :action => ''index'' else render :text => "EMPTY>>????" #render :action => ''new'' end end ok, it seems that it is now getting to the empty statement, my only problem now is that how do i redirect this action to redisplay the form. the form.rhtml is in a directory called displays. in the form file, i have case stament that calls the partial for the type of form. so i have a partial for cars, jobs, food, etc... when i tried the render option, it looks for form in my controller folder. can the validate be done like this? -- 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 -~----------~----~----~----~------~----~------~--~---
Chris T
2006-Dec-03 19:21 UTC
Re: validates_presence_of not working...could it be my parti
koloa wrote:> hi > def create > @job = Job.new(params[:temp]) > if @job.save > flash[:notice] = ''Profile was successfully created.'' > redirect_to :action => ''index'' > else > render :text => "EMPTY>>????" > #render :action => ''new'' > end > end > > > > ok, it seems that it is now getting to the empty statement, my only > problem now is that how do i redirect this action to redisplay the form. > > the form.rhtml is in a directory called displays. in the form file, i > have case stament that calls the partial for the type of form. so i have > a partial for cars, jobs, food, etc... > > when i tried the render option, it looks for form in my controller > folder. > > can the validate be done like this? > > >I think part of the problem is you''re trying to do things the non-Rails way -- which can be done but it does make things more difficult. There are two idiomatic Rails ways of doing something like this -- the new 1.2 REST-ful way, and the old verb-based action way. Either way, Rails works much of its magic by assumptions on your naming convetions -- you can override them, but it''s so much easier if you don''t have to. Assuming you haven''t got into the RESTful goodness yet -- and it''s worth looking at), I would suggest something like the following (not tested, and I''ve made some possibly incorrect assumptions about your models): AllPurposeController < ApplicationController def new @item = params[:fid].constantize.new #instantiate new item of class params[:fid], e.g. "Job" @ddvd_cover = Repicture.new if params[:fid] == "House" end end def create @item = params[:fid].constantize.new(params[:item]) if @item.save flash[:notice] = ''Profile was successfully created.'' redirect_to :action => ''index'' else render :action => ''new'' end end form.rhtml <%= error_messages_for ''item'' %> <!--[form:item]--> <%= start_form_tag :action => ''create'', :fid => @item.class.to_s %> # put stuff here that''s common to all items <%= render :partial => ''displays/#{@item.class.to_s.downcase}_form'' %> # can your form_partials "job_form", "house_form", etc <%= submit_tag "Create"%> <%= end_form_tag%> _job_form.rhtml <p><label for="item_company">Company</label><br/> <%= text_field :item, ''company'' %></p> <p><label for="item_description">Description</label><br/> <%= text_area :item, ''description'', :cols => "30", :rows=> "10" %></p> ...etc... <!--[eoform:temp]--> modelJob class Job < ActiveRecord::Base validates_presence_of : company, : description, :on => :create, :message => "can''t be blank" end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
wow, chris, thank you so much for much detail response. man, everytime i learn more about ruby rails, im always shaving off more and more code. after i completed my first application, i noticed i can reduce the code by 80% doing more OO. after i did that, i looked at your code, and notice by using constantize i can trim probably another 20%. hotdamn, before i know it, my application will be about 10% the size as when i first started coding the application! thanks again. Chris T wrote:> koloa wrote: >> end >> when i tried the render option, it looks for form in my controller >> folder. >> >> can the validate be done like this? >> >> >> > I think part of the problem is you''re trying to do things the non-Rails > way -- which can be done but it does make things more difficult. > There are two idiomatic Rails ways of doing something like this -- the > new 1.2 REST-ful way, and the old verb-based action way. Either way, > Rails works much of its magic by assumptions on your naming convetions > -- you can override them, but it''s so much easier if you don''t have to. > > Assuming you haven''t got into the RESTful goodness yet -- and it''s worth > looking at), I would suggest something like the following (not tested, > and I''ve made some possibly incorrect assumptions about your models): > > AllPurposeController < ApplicationController > > def new > @item = params[:fid].constantize.new #instantiate new item of class > params[:fid], e.g. "Job" > @ddvd_cover = Repicture.new if params[:fid] == "House" > end > end > > def create > @item = params[:fid].constantize.new(params[:item]) > if @item.save > flash[:notice] = ''Profile was successfully created.'' > redirect_to :action => ''index'' > else > render :action => ''new'' > end > end > > > form.rhtml > > <%= error_messages_for ''item'' %> > <!--[form:item]--> > <%= start_form_tag :action => ''create'', :fid => @item.class.to_s %> > # put stuff here that''s common to all items > <%= render :partial => ''displays/#{@item.class.to_s.downcase}_form'' %> > # can your form_partials "job_form", "house_form", etc > <%= submit_tag "Create"%> > <%= end_form_tag%> > > > > _job_form.rhtml > > <p><label for="item_company">Company</label><br/> > <%= text_field :item, ''company'' %></p> > > <p><label for="item_description">Description</label><br/> > <%= text_area :item, ''description'', :cols => "30", :rows=> "10" %></p> > ...etc... > <!--[eoform:temp]--> > > > modelJob > class Job < ActiveRecord::Base > validates_presence_of : company, : description, :on => :create, > :message => "can''t be blank" > 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 -~----------~----~----~----~------~----~------~--~---
Chris T
2006-Dec-03 19:45 UTC
Re: validates_presence_of not working...could it be my parti
koloa wrote:> > wow, chris, thank you so much for much detail response. man, everytime i > learn more about ruby rails, im always shaving off more and more code. > after i completed my first application, i noticed i can reduce the code > by 80% doing more OO. after i did that, i looked at your code, and > notice by using constantize i can trim probably another 20%. hotdamn, > before i know it, my application will be about 10% the size as when i > first started coding the application! > > thanks again. > > > > > Chris T wrote: > >> koloa wrote: >> >>> end >>> when i tried the render option, it looks for form in my controller >>> folder. >>> >>> can the validate be done like this? >>> >>> >>> >>> >> I think part of the problem is you''re trying to do things the non-Rails >> way -- which can be done but it does make things more difficult. >> There are two idiomatic Rails ways of doing something like this -- the >> new 1.2 REST-ful way, and the old verb-based action way. Either way, >> Rails works much of its magic by assumptions on your naming convetions >> -- you can override them, but it''s so much easier if you don''t have to. >> >> Assuming you haven''t got into the RESTful goodness yet -- and it''s worth >> looking at), I would suggest something like the following (not tested, >> and I''ve made some possibly incorrect assumptions about your models): >> >> AllPurposeController < ApplicationController >> >> def new >> @item = params[:fid].constantize.new #instantiate new item of class >> params[:fid], e.g. "Job" >> @ddvd_cover = Repicture.new if params[:fid] == "House" >> end >> end >> >> def create >> @item = params[:fid].constantize.new(params[:item]) >> if @item.save >> flash[:notice] = ''Profile was successfully created.'' >> redirect_to :action => ''index'' >> else >> render :action => ''new'' >> end >> end >> >> >> form.rhtml >> >> <%= error_messages_for ''item'' %> >> <!--[form:item]--> >> <%= start_form_tag :action => ''create'', :fid => @item.class.to_s %> >> # put stuff here that''s common to all items >> <%= render :partial => ''displays/#{@item.class.to_s.downcase}_form'' %> >> # can your form_partials "job_form", "house_form", etc >> <%= submit_tag "Create"%> >> <%= end_form_tag%> >> >> >> >> _job_form.rhtml >> >> <p><label for="item_company">Company</label><br/> >> <%= text_field :item, ''company'' %></p> >> >> <p><label for="item_description">Description</label><br/> >> <%= text_area :item, ''description'', :cols => "30", :rows=> "10" %></p> >> ...etc... >> <!--[eoform:temp]--> >> >> >> modelJob >> class Job < ActiveRecord::Base >> validates_presence_of : company, : description, :on => :create, >> :message => "can''t be blank" >> end >> > > >No prob. I came from PHP background so I know the feeling. One tip: start testing (either Test First or Test Driven) as soon as poss -- it took me a while to catch on to it, but now it''s a joy to refactor apps because it. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---