# Model validation outside of the model

**URL:** <https://www.ruby-forum.com/t/model-validation-outside-of-the-model/143143>\
**Category:** Rails\
**Created:** [July 15, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/model-validation-outside-of-the-model/143143 "2008-07-15T17:29:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 15, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/model-validation-outside-of-the-model/143143/1 "2008-07-15T17:29:30Z")

</div>

Hi, having trouble validating my form:

\_form.html.erb:  
\<% form\_remote\_for :user, @user, :url =\> { :action =\> “create” } do  
|f| %\>

  
  
  
  
  
  
  

```
<tr>
  <td class="text">Password:</td>
  <td><%= f.text_field :password %></td>
</tr>

  <tr>
    <td></td>
    <td align="right">
        <%= submit_tag "Add user" %>
    </td>
  </tr>

</table>

```

\<% end %\>

controller:  
def create  
@user = User.new(params[:user])

```
if @user.save
 if(request.xhr?)
  render :update do |rjs|
      flash[:notice] ="<font class='notice-obj'>" + @user.email +

```

" was added successfully!"

```
      rjs.replace_html "notice", :partial => 'notice'
      rjs.visual_effect(:highlight, "notice", :startcolor =>

```

“#D6D6D6”, :endcolor =\> “#ffffff”, :duration =\> 2)  
end  
else  
redirect\_to\_index and return  
end  
end  
end

On the surface, it seems that if a user enters invalid info into the  
fields, it doesn’t make it to the redirect\_to\_index at all. In fact,  
when I put a flash[:notice] into the else and submit invalid info, it  
doesn’t make it. That’s problem #1.

I guess problem #2 follows #1. Where would I put \<%= error\_messages\_for  
‘user’ %\>, if that’s the right way to do it, firstly.

Thanks!

| 
  

  |
| Email: | \<%= f.text\_field :email %\> |

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 15, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/model-validation-outside-of-the-model/143143/2 "2008-07-15T19:50:37Z")

</div>

> On the surface, it seems that if a user enters invalid info into the  
> fields, it doesn’t make it to the redirect\_to\_index at all. In fact,  
> when I put a flash[:notice] into the else and submit invalid info, it  
> doesn’t make it. That’s problem #1.

Have you read through your code? cutting out the fluff it does

if @user.save  
…  
end  
i.e. nothing happens if the user isn’t valid. Looks like you flipped  
the request.xhr? test with the user validity one

> I guess problem #2 follows #1. Where would I put \<%= error\_messages\_for  
> ‘user’ %\>, if that’s the right way to do it, firstly.

In your notice partial.

Fred

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 15, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/model-validation-outside-of-the-model/143143/3 "2008-07-15T23:18:17Z")

</div>

def create  
@user = User.new(params[:user])

```
if @user.save
 if(request.xhr?)
  render :update do |rjs|
      flash[:notice] ="<font class='notice-obj'>" + @user.email + 

```

" was added successfully!"

```
      rjs.replace_html "notice", :partial => 'notice'
      rjs.visual_effect(:highlight, "notice", :startcolor => 

```

“#D6D6D6”, :endcolor =\> “#ffffff”, :duration =\> 2)  
end  
else  
redirect\_to\_index and return  
end  
else  
if(request.xhr?)  
render :update do |rjs|  
rjs.replace\_html “notice”, :partial =\> ‘notice’, :object =\>  
@user  
#rjs.visual\_effect(:highlight, “notice”, :startcolor =\>  
“#FF8888”, :endcolor =\> “#ffffff”, :duration =\> 5)  
end  
end  
end  
end

partial:  
\<%= flash[:notice] if flash[:notice] %\>  
\<%= @user.errors %\>

I get # for errors ?

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 16, 2008, 8:16am UTC](https://www.ruby-forum.com/t/model-validation-outside-of-the-model/143143/4 "2008-07-16T08:16:46Z")

</div>

> partial:  
> \<%= flash[:notice] if flash[:notice] %\>  
> \<%= @user.errors %\>
> 
> I get # for errors ?

You can’t just dump a ruby object into your html like that (apart from  
anything else the default string representation on an object has \<\> in  
it, which confuses browsers. That’s what error\_messages\_for is for (or  
build your own replacement)

Fred
