I have a form with only one field called secretcode to submit. But when I submit it, the data is saved. But I need to do form validation for this field. I have given validates_presence_of :secret in the Secretcode Model. when I submit it without entering anything in secret code field, then it is showing as "undefined method `model_name'' for NilClass:Class".. Please help. I am learning rails by doing.. new.html.erb> <%=form_for(@secretcode) do |f|%> <%= render ''layouts/error'' %> > <%=f.label :secret%> <%= f.text_field :secret %> > > <%=f.submit :generate%> <%end%>Secretcode Model> class Secretcode < ActiveRecord::Base belongs_to :user > attr_accessible :secret validates(:secret, :presence=> true) endsecretcodes controller> def create > @code= Secretcode.new(params[:secretcode]) > if @code.save > p "It is saved..........." > redirect_to @code > else > render ''new'' > 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-/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 https://groups.google.com/groups/opt_out.
On 16 October 2012 11:23, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have a form with only one field called secretcode to submit. But when > I submit it, the data is saved. But I need to do form validation for > this field. I have given validates_presence_of :secret in the Secretcode > Model. when I submit it without entering anything in secret code field, > then it is showing as "undefined method `model_name'' for > NilClass:Class".. Please help. I am learning rails by doing..You say that if the field is empty you get the error. Do you get the error if there is something in the field? If you look at the error message and stack trace it may tell you where in your code it is failing. Proibably something that you have setup is nil. If you still can''t see the problem then post the full error message and stack trace and tell us which line of your code it is referencing (if any).> > new.html.erb > >> <%=form_for(@secretcode) do |f|%> <%= render ''layouts/error'' %> >> <%=f.label :secret%> <%= f.text_field :secret %> >> >> <%=f.submit :generate%> <%end%> > > Secretcode Model > >> class Secretcode < ActiveRecord::Base belongs_to :user >> attr_accessible :secret validates(:secret, :presence=> true) endI hope there are some newines in the above> > secretcodes controller > > > >> def create >> @code= Secretcode.new(params[:secretcode]) >> if @code.save >> p "It is saved..........." >> redirect_to @code >> else >> render ''new'' >> endCan you show us the code for the new action in the controller also please? Colin -- 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 https://groups.google.com/groups/opt_out.
Colin Law wrote in post #1079996: On 16 October 2012 11:23, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: I have a form with only one field called secretcode to submit. But when I submit it, the data is saved. But I need to do form validation for this field. I have given validates_presence_of :secret in the Secretcode Model. when I submit it without entering anything in secret code field, then it is showing as "undefined method `model_name'' for NilClass:Class".. Please help. I am learning rails by doing.. You say that if the field is empty you get the error. Do you get the error if there is something in the field? If you look at the error message and stack trace it may tell you where in your code it is failing. Proibably something that you have setup is nil. If you still can''t see the problem then post the full error message and stack trace and tell us which line of your code it is referencing (if any). class Secretcode < ActiveRecord::Base belongs_to :user attr_accessible :secret validates(:secret, :presence=> true) end I hope there are some newines in the above else render ''new'' end Can you show us the code for the new action in the controller also please? Colin>def new > @secretcode=Secretcode.new > 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 https://groups.google.com/groups/opt_out.
On 16 October 2012 12:49, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1079996: > On 16 October 2012 11:23, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > I have a form with only one field called secretcode to submit. But when > I submit it, the data is saved. But I need to do form validation for > this field. I have given validates_presence_of :secret in the > Secretcode > Model. when I submit it without entering anything in secret code field, > then it is showing as "undefined method `model_name'' for > NilClass:Class".. Please help. I am learning rails by doing.. > > You say that if the field is empty you get the error. Do you get the > error if there is something in the field? > > If you look at the error message and stack trace it may tell you where > in your code it is failing. Proibably something that you have setup > is nil. If you still can''t see the problem then post the full error > message and stack trace and tell us which line of your code it is > referencing (if any). > > class Secretcode < ActiveRecord::Base belongs_to :user > attr_accessible :secret validates(:secret, :presence=> true) end > > I hope there are some newines in the above > > else > render ''new'' > end > > Can you show us the code for the new action in the controller also > please? > > Colin > >>def new >> @secretcode=Secretcode.new >> endAnd the other questions I asked? Colin -- 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 https://groups.google.com/groups/opt_out.