Model validation outside of the model

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 %>

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

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 ?

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