Matthew Hillsborough
2010-Apr-23 00:23 UTC
how to display error messages for a child resource that failed validation
Can anyone explain why this happens?
mybox:$ ruby script/console
Loading development environment (Rails 2.3.5)
>> foo = Foo.new
=> #<Foo id: nil, customer_id: nil, created_at: nil, updated_at:
nil>
>> bar = Bar.new
=> #<Bar id: nil, bundle_id: nil, alias: nil, real: nil, active:
true, list_type: 0, body_record_active: false, created_at: nil,
updated_at: nil>
>> bar.save
=> false
>> bar.errors.each_full { |msg| puts msg }
Real can''t be blank
Real You must supply a valid email
=> ["Real can''t be blank", "Real You must supply
a valid email"]
So far that is perfect, that is what i want the error message to read.
Now for more:
>> foo.bars << bar
=> [#<Bar id: nil, bundle_id: nil, alias: nil, real: nil, active:
true, list_type: 0, body_record_active: false, created_at: nil,
updated_at: nil>]
>> foo.save
=> false
>> foo.errors.to_xml
=> "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n<errors>\n
<error>Bars is invalid</error>\n</errors>\n"
That is what I can''t figure out. Why am I getting Bars is invalid
versus the error messages displayed above, ["Real can''t be
blank",
"Real you must supply a valid email"] etc.
My controller simply has a respond_to method with the following in it:
format.xml { render :xml => @foo.errors, :status
=> :unprocessable_entity }
How do I have this output the real error messages so the user has some
insight into what they did wrong? How do I write my render method in
my controller to show all of the appropriate error messages? And more
important, what will my xml builder view look?
--
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.
Michael Pavling
2010-Apr-23 09:49 UTC
Re: how to display error messages for a child resource that failed validation
On 23 April 2010 01:23, Matthew Hillsborough <matthew.hillsborough-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That is what I can''t figure out. Why am I getting Bars is invalid > versus the error messages displayed above, ["Real can''t be blank", > "Real you must supply a valid email"] etc. > > How do I have this output the real error messages so the user has some > insight into what they did wrong? How do I write my render method in > my controller to show all of the appropriate error messages? And more > important, what will my xml builder view look? >I tend to use the "merge errors" process gleaned from http://dev.rubyonrails.org/attachment/ticket/11394/merge_bang_errors.patch http://gist.github.com/376389 Then in your controller you can use: @foo.errors.merge! bar.errors ... and the view will work as normal, but with nice errors :-) -- 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.
Matthew Hillsborough
2010-Apr-23 13:57 UTC
Re: how to display error messages for a child resource that failed validation
Michael, Thank you for this, I will try it right away. Mind if I ask where the best place to put this code in as a best practice for Rails? Matthew On Fri, Apr 23, 2010 at 5:49 AM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 23 April 2010 01:23, Matthew Hillsborough > <matthew.hillsborough-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > That is what I can''t figure out. Why am I getting Bars is invalid > > versus the error messages displayed above, ["Real can''t be blank", > > "Real you must supply a valid email"] etc. > > > > How do I have this output the real error messages so the user has some > > insight into what they did wrong? How do I write my render method in > > my controller to show all of the appropriate error messages? And more > > important, what will my xml builder view look? > > > > I tend to use the "merge errors" process gleaned from > http://dev.rubyonrails.org/attachment/ticket/11394/merge_bang_errors.patch > > http://gist.github.com/376389 > > Then in your controller you can use: > > @foo.errors.merge! bar.errors > > ... and the view will work as normal, but with nice errors :-) > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
oren
2010-May-07 19:44 UTC
Re: how to display error messages for a child resource that failed validation
I am not sure about best practice but I created a file config/ initializers/active_record.rb with the content of http://gist.github.com/376389 and reload the server. as far as I understand, the initializers folder is the place to have stuff you want to load at startup for all environments. And what Michael''s code is doing is opening the class Errors inside ActiveRecord module and adding the method merge! please correct me if it''s not accurate. also, where can I find more insights about the initialize process? On Apr 23, 1:57 pm, Matthew Hillsborough <matthew.hillsboro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Michael, > > Thank you for this, I will try it right away. > > Mind if I ask where the best place to put this code in as a best practice > for Rails? > > Matthew > > > > On Fri, Apr 23, 2010 at 5:49 AM, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 23 April 2010 01:23, Matthew Hillsborough > > <matthew.hillsboro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > That is what I can''t figure out. Why am I getting Bars is invalid > > > versus the error messages displayed above, ["Real can''t be blank", > > > "Real you must supply a valid email"] etc. > > > > How do I have this output the real error messages so the user has some > > > insight into what they did wrong? How do I write my render method in > > > my controller to show all of the appropriate error messages? And more > > > important, what will my xml builder view look? > > > I tend to use the "merge errors" process gleaned from > >http://dev.rubyonrails.org/attachment/ticket/11394/merge_bang_errors.... > > >http://gist.github.com/376389 > > > Then in your controller you can use: > > > -BwrsK8dMMIYXjZzgnn0Flg@public.gmane.org! bar.errors > > > ... and the view will work as normal, but with nice errors :-) > > > -- > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > 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 athttp://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.
oren
2010-May-07 22:09 UTC
Re: how to display error messages for a child resource that failed validation
I improved this solution by adding a method that removes the rails
default message (''child model is invalid'')
but keeps all the rest:
(notice the call to merge! at the end)
show_child_errors(@key, value, "translation_values")
def show_child_errors(parent, child, collection_name)
errors = parent.errors.each{|attr,msg| attr }
if errors.include?(collection_name)
#keep all other errors
keep_errors = parent.errors.select{|attr,msg| attr !collection_name }
parent.errors.clear
keep_errors.each do |e|
parent.errors.add(e.first, e.second)
end
parent.errors.merge!(child.errors)
end
end
I am not
On May 7, 7:44 pm, oren
<orengo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I am not sure about best practice but I created a file config/
> initializers/active_record.rb
> with the content ofhttp://gist.github.com/376389and reload the
> server.
>
> as far as I understand, the initializers folder is the place to have
> stuff you want to load at startup for all environments.
> And what Michael''s code is doing is opening the class Errors
inside
> ActiveRecord module and adding the method merge!
>
> please correct me if it''s not accurate.
> also, where can I find more insights about the initialize process?
>
> On Apr 23, 1:57 pm, Matthew Hillsborough
>
>
>
> <matthew.hillsboro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Michael,
>
> > Thank you for this, I will try it right away.
>
> > Mind if I ask where the best place to put this code in as a best
practice
> > for Rails?
>
> > Matthew
>
> > On Fri, Apr 23, 2010 at 5:49 AM, Michael Pavling
<pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > On 23 April 2010 01:23, Matthew Hillsborough
> > >
<matthew.hillsboro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > > That is what I can''t figure out. Why am I getting
Bars is invalid
> > > > versus the error messages displayed above, ["Real
can''t be blank",
> > > > "Real you must supply a valid email"] etc.
>
> > > > How do I have this output the real error messages so the
user has some
> > > > insight into what they did wrong? How do I write my render
method in
> > > > my controller to show all of the appropriate error messages?
And more
> > > > important, what will my xml builder view look?
>
> > > I tend to use the "merge errors" process gleaned from
> >
>http://dev.rubyonrails.org/attachment/ticket/11394/merge_bang_errors....
>
> > >http://gist.github.com/376389
>
> > > Then in your controller you can use:
>
> > > -BwrsK8dMMIYXjZzgnn0Flg@public.gmane.org! bar.errors
>
> > > ... and the view will work as normal, but with nice errors :-)
>
> > > --
> > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm.
> > > To unsubscribe from this group, send email to
> > >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> > > .
> > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > For more options, visit this group
athttp://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
athttp://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.
oren
2010-May-07 22:09 UTC
Re: how to display error messages for a child resource that failed validation
I improved this solution by adding a method that removes the rails
default message (''child model is invalid'')
but keeps all the rest:
(notice the call to merge! at the end)
show_child_errors(@key, value, "translation_values")
def show_child_errors(parent, child, collection_name)
errors = parent.errors.each{|attr,msg| attr }
if errors.include?(collection_name)
#keep all other errors
keep_errors = parent.errors.select{|attr,msg| attr !collection_name }
parent.errors.clear
keep_errors.each do |e|
parent.errors.add(e.first, e.second)
end
parent.errors.merge!(child.errors)
end
end
I am not
On May 7, 7:44 pm, oren
<orengo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I am not sure about best practice but I created a file config/
> initializers/active_record.rb
> with the content ofhttp://gist.github.com/376389and reload the
> server.
>
> as far as I understand, the initializers folder is the place to have
> stuff you want to load at startup for all environments.
> And what Michael''s code is doing is opening the class Errors
inside
> ActiveRecord module and adding the method merge!
>
> please correct me if it''s not accurate.
> also, where can I find more insights about the initialize process?
>
> On Apr 23, 1:57 pm, Matthew Hillsborough
>
>
>
> <matthew.hillsboro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Michael,
>
> > Thank you for this, I will try it right away.
>
> > Mind if I ask where the best place to put this code in as a best
practice
> > for Rails?
>
> > Matthew
>
> > On Fri, Apr 23, 2010 at 5:49 AM, Michael Pavling
<pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > On 23 April 2010 01:23, Matthew Hillsborough
> > >
<matthew.hillsboro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > > That is what I can''t figure out. Why am I getting
Bars is invalid
> > > > versus the error messages displayed above, ["Real
can''t be blank",
> > > > "Real you must supply a valid email"] etc.
>
> > > > How do I have this output the real error messages so the
user has some
> > > > insight into what they did wrong? How do I write my render
method in
> > > > my controller to show all of the appropriate error messages?
And more
> > > > important, what will my xml builder view look?
>
> > > I tend to use the "merge errors" process gleaned from
> >
>http://dev.rubyonrails.org/attachment/ticket/11394/merge_bang_errors....
>
> > >http://gist.github.com/376389
>
> > > Then in your controller you can use:
>
> > > -BwrsK8dMMIYXjZzgnn0Flg@public.gmane.org! bar.errors
>
> > > ... and the view will work as normal, but with nice errors :-)
>
> > > --
> > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm.
> > > To unsubscribe from this group, send email to
> > >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> > > .
> > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > For more options, visit this group
athttp://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
athttp://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.