Sunday, June 12, 2011

Fixing the delete method in Jquery

Here's a little problem I ran into the other day. I've switched to Jquery from Prototype, and while I was initially able to get Javascript working with Jquery, my :delete method stopped working. Rather than destroying the thing it was called to destroy, it redirected me to the show page.

After a few google searches, I found this is a fairly common problem. However, none of the solutions I found fixed my problem entirely, but a combination of suggestions DID fix the problem.

I'd installed the jquery gem, as suggested here but the problem was in the javascript_includ_tag in the header of my application.html.erb file.

At first I had <%= javascript_include_tag :defaults %>, which I assumed would work since Jquery was the new default Javascript library after installing the gem, but I had to edit it like so:

<%= javascript_include_tag :defaults, 'jquery_ujs' %>

Bingo! Once again, my :delete method works, and my flash[:notice] script is working, too.

Now for a little question.

You may recall my previous question about passing multiple values to a hidden_field. David showed me this code:

  def status
    finished? ? 'finished' : 'unfinished'

This goes in the Task model and defines these two statuses. However, I don't know how to call them and mark a Task as finished or unfinished then have that change updated in the view.

Here's the code I've got now:

<%= form_for(task) do |f| %>
  <%= f.hidden_field :finished, :value => !task.finished %>
  <div class="actions">
    <% if task.finished %>
      <%= image_submit_tag 'finished.png' %>
    <% else %>
      <%= image_submit_tag 'unfinished.png' %>
  <% end %>

Basically, I want it to work this way: A new Task is defined as unfinished by default, a user can mark a task as finished by clicking on 'unfinished.png', or mark it as unfinished again by clicking on 'finished.png'. This change should mark the Task with that status, either 'finished' or 'unfinished.'

Will I need to update the table for Task and add this as a field? Or can this information be stored in the database just by using the hidden_field? I want to be able to manipulate Tasks based on whether they're marked as finished or unfinished later.

I didn't do much coding this weekend, but it definitely felt good to sit down and fix my Jquery problem after thinking about it most of the day today.


  1. You shouldn't have to change anything in your form.

    Your controller should be changed to somehing like this:

    def update
    @task = Task.find(params[:id]
    if @task.update_attributes(params[:task])
    redirect_to(tasks_path, :notice => "Task was marked as #{@task.status}")
    redirect_to(tasks_path, :notice => "Could not update task"

    You should really push your code to Github more often. It's a lot easier to help when I know exactly what code you've got in front of you right now.

  2. @David Awesome, thanks for the reply! I've been meaning to push my code. I got hung up when I was working on the javascript for the flash notices, then Jquery stopped working for me. I'll push the changes over when I get home tonight.