0

I have created an app that performs all CRUD operations remotely using Jquery. In my views i have an index.html.erb that houses the form and a table that shows the records. My problem is when I submit the form with errors, I get no feedback. I believe my problem lies in my create.js.coffee file. I am using authlogic to handle validation on some portions of the form.

in create.js.coffee

$('<%= escape_javascript(render(:partial => @user))%>')
  .appendTo('#user_table')
  .hide()
  .fadeIn(200)

$('#new_user')[0].reset()

$('#users_count').html '<%= users_count %>'

in index.html.erb

<% title "Create New User for XX" %>
<div id="paginate"><%= paginate @users, :remote => true %></div>
<div id="users_count"><strong><%= users_count %></strong></div>
<div id="users">
    <table id="user_table">
       <tr>
           <th>Id</th>
           <th>First</th>
           <th>Last</th>
           <th>Email</th>
           <th>Last Login</th>
           <th>Created At</th>
           <th>Del</th>
           <th>Edit</th>
       </tr>
<%=render @users %>
</table>
</div>

<h3> Add new user below </h3>
<%= render :partial => 'form' %>

in _form.html.erb

<%= form_for User.new, :remote => true do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :fname %><br />
    <%= f.text_field :fname %>
  </p>
  <p>
    <%= f.label :lname %><br />
    <%= f.text_field :lname %>
  </p>

  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>

  <p>
    <%= f.label :admin %><br />
    <%= f.check_box :admin %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>

  <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </p>
  <p><%= f.submit %></p>
<% end %>

1 Answer 1

5

It was simple as this. create an errors partial and an errors div in the view and....

in create.js.coffee

<% if @user.errors.any? %>
$('<%= escape_javascript(render :partial => "errors", :locals => {:target => @user })%>')
   .appendTo('#errors')
<% else %>
$('<%= escape_javascript(render(:partial => @user))%>')
  .appendTo('#user_table')
  .hide()
  .fadeIn(200)

$('#new_user')[0].reset()

$('#users_count').html '<%= users_count %>'
<% end %>
$('#error_clo
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.