Handling validation

Hi list,

I’ve started migrating to rails 3 and right now I’m getting to grips
with the
different methods for handling validation error message.

I’ve found this on asciiguides which seems good, the first code examples
which move
the error display handling to a partial:

http://asciicasts.com/episodes/211-validations-in-rails-3

Which works perfectly for the new and create actions, however on the
edit and update
actions it gives me the following, referring to the first line of the
partial:

undefined local variable or method `target’ for
#<#Class:0xb6459528:0xb645683c>

The only difference between the new/create and the edit/update actions
is the formers
uses @model.save and the latter @model.update_attributes.

If I take out the refence to the partial, replacing the model name back
in, everything
works fine but really isn’t DRY.

I can post full code examples if necessary.

Thanks

Matt

On Sun, Apr 17, 2011 at 5:01 PM, Matt H.
<[email protected]

wrote:

I can post full code examples if necessary.

That would be extremely helpful.

B.

On Mon, Apr 18, 2011 at 07:51:49AM -0500, Bryan C. wrote:

On Sun, Apr 17, 2011 at 5:01 PM, Matt H. <[email protected]

wrote:

I can post full code examples if necessary.

That would be extremely helpful.

Ok, here is the controller code:

http://pastie.org/1807523

And the views:

http://pastie.org/1807530

And the partial:

http://pastie.org/1807536

new/update works perfectly, while edit/update gives this error while
visiting the edit
action:

http://pastie.org/1807548

If I change the edit view to look like the following, it works fine:

http://pastie.org/1807558

But I don’t want to have to do that for every view. As you can tell I’m
no expert and
I’m a bit stumped. Grateful for some tips

Thanks

On Mon, Apr 18, 2011 at 10:44:08PM -0500, Bryan C. wrote:

= render “shared/error_messages”, :target => @category

B.

Thanks Bryan,

That was exactly the problem. I had looked at the edit views vs the new
views so many
times. I just didn’t notice that they differed as you showed above. I
thought I’d
copied and pasted so I really didn’t expect them to be different.

Thanks again

On Mon, Apr 18, 2011 at 10:47 AM, Matt H. <
[email protected]> wrote:

http://pastie.org/1807536

http://pastie.org/1807558

But I don’t want to have to do that for every view. As you can tell I’m no
expert and
I’m a bit stumped. Grateful for some tips

Thanks

I think the issue is that you need to remove :partial=> from your Edit
Category view. When you are specifying :partial hash param it is
ignoring
:target and expecting something else, like collection, to be passed in.
If
you change your Edit to be like what is below it should work.

Change from this:

= render :partial => “shared/error_messages”, :target => @category

To this:

= render “shared/error_messages”, :target => @category

B.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs