Take the 2-minute tour ×

I'm new to rails and ajax so I'm experimenting by building a simple app. I created a simple to do app with User and Tasks (user has many tasks). Using Devise to handle the authentication. I set up the task CRUD and implemented best_in_place (in place editing gem) and got it to work. What I wanted to do next is add ajax create and delete following some example resources such as creating a 100% ajax CRUD. After setting up all the necessary code for the create and destroy in tasks_controller, I'm getting DELETE http://localhost:3000/tasks/26 500 (Internal Server Error) as seen in the firebug console log. These are the errors printed in the console in firebug:

(anonymous function)

when the destroy link is clicked and same with create. The task got deleted and created after refresh but the ajax is just not working. I spent some time trying to figure out what's wrong but don't have any luck yet so I'm hoping I can get some help here while I keep digging into it. Below are my setups:

First the controller code:

def destroy
    @task = current_user.tasks.find(params[:id])
    flash[:notice] = "Successfully destroyed post."
        respond_to do |format|
            format.html { }
            format.js { }

def create 
    @task = current_user.tasks.build(params[:task])

    respond_with(@task) do |format|
        if @task.save
            flash[:notice] = "Task was created successfully!"
            respond_to do |format|
            format.html { redirect_to tasks_url }
            format.xml { render :xml => @task }
            format.json { render :json => @task}
            format.html { render :action => :new }


The task list partial to display the task list and delete

<% @tasks.each do |task| %>

<td><%= best_in_place task, :detail %></td>
<!-- <p><%= best_in_place task, :completed, type: :checkbox %></p> -->
 <td><%= link_to "Destroy", task, :confirm => 'Are you sure?', :method => :delete, :remote => true, :id=>'delete' %></td>

<% end %>

The create form (without the other view stuff):

true) do |f| %>

<%= f.label :detail %>
<%= f.text_field :detail %> <%= f.submit %> <% end %>

This is the javascript tag for application.html.erb (include jquery.js, jquery.ujs, application.js and other not so related js files):

Lastly I'm just trying to call an alert inside destroy.js.erb and with create.js.erb:

 alert("Task was deleted");

Again, the crud is working normally when refreshed, it feels like the destroy.js.erb and create.js.erb is not linked somehow. I appreciated if you can provide a few hints.

Here is the error from the console after hitting delete:

ActionView::Template::Error (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each):
    2: <table>
    3: <% @tasks.each do |task| %>
    5: <tr>
    6: <td><%= best_in_place task, :detail %></td>
  app/views/tasks/_tasks.html.erb:3:in `_app_views_tasks__tasks_html_erb___1747031968745406293_70257861453000'
  app/views/tasks/destroy.js.erb:4:in `_app_views_tasks_destroy_js_erb___3673072470672404292_70257861472560'
share|improve this question
Figured out what the problem was. There was an undefined variable @tasks that was not added after moving the create and destroy into the index. And that undefined variable was causing jquery to break (although the action works). –  Poyi Jul 27 '12 at 20:54
add comment

1 Answer

It looks as though you have a syntax error in the destroy action. I think you're missing an end for the respond_to block.

share|improve this answer
Sorry it looks like when I copy the code over for the destroy action I missed an end. In the code it does have a closing end for the respond_to. –  Poyi Jul 27 '12 at 16:52
add comment

Your Answer


