Rails Auth Token und Ajax

Okay, nach dem, was ich auf anderen Websites und beim Überlaufen des Stacks gelesen habe, gibt Rails diesen Authentifizierungs-Token-Fehler aus, da mein Formular das Token nicht übergibt - und es ist eine Sicherheitsfunktion. Das verstehe ich.

Allerdings habe ich keine wirkliche Form. Ich habe Ajax hier - mein Javascript postet die identifizierten Informationen in eine Verarbeitungsfunktion.

Meine Frage lautet also: Wie erhalte ich das Authentifizierungstoken für meinen Controller?

Meine Ansicht sieht so aus:

<% for transaction in @transactions %>
        <% if transaction["category"] == '' %>
        <% transaction["category"] = "Uncategorized" %>
                        <% end %>

                        <tr title = "<% if params[:type] %><%= params[:type] %><% else %>Purchases<% end %> <%= transaction["id"] %>" >

                            <td class="check"><a class="help" href="#"><img src="/images/icons/help.png" alt="?" /></a><input type="checkbox" /></td>
                            <td class="date"><% if transaction["date"] != "0000-00-00 00:00:00" %><%= transaction["date"].to_date.strftime("%B %d") %><% end %></td>

                            <% if params[:type] == "Bills" || params[:type] == "Reimbursements" %>
                            <td class="payee"><%= transaction["payee"] %></td>
                            <td class="details"><%= transaction["details"] %></td>
                            <% else %>
                            <td class="description"><% if transaction["detail"] == "undefined" %>n/a<% else %><%= transaction["detail"] %><% end %></td>
                            <td class="category">n/a</td>
                            <% end %>

                            <td class="amount">-
<% for transaction in @transactions %>
        <% if transaction["category"] == '' %>
        <% transaction["category"] = "Uncategorized" %>
                        <% end %>

                        <tr title = "<% if params[:type] %><%= params[:type] %><% else %>Purchases<% end %> <%= transaction["id"] %>" >

                            <td class="check"><a class="help" href="#"><img src="/images/icons/help.png" alt="?" /></a><input type="checkbox" /></td>
                            <td class="date"><% if transaction["date"] != "0000-00-00 00:00:00" %><%= transaction["date"].to_date.strftime("%B %d") %><% end %></td>

                            <% if params[:type] == "Bills" || params[:type] == "Reimbursements" %>
                            <td class="payee"><%= transaction["payee"] %></td>
                            <td class="details"><%= transaction["details"] %></td>
                            <% else %>
                            <td class="description"><% if transaction["detail"] == "undefined" %>n/a<% else %><%= transaction["detail"] %><% end %></td>
                            <td class="category">n/a</td>
                            <% end %>

                            <td class="amount">-$<%= transaction["amount"] %></td>
                        </tr>

                    <% end %>
lt;%= transaction["amount"] %></td> </tr> <% end %>

Die entsprechende Ajax lautet wie folgt:

/* send ids by ajax */
$('#tableActions li a').click(function() { 
    if(!$(this).hasClass('disabled')) {
        action = $(this).text();
        ids = new Array();
        i = 0;
        $('td.check input','#tableHolder').each(function() { if($(this).attr('checked')) { ids[i++] = $(this).parents('tr').attr('title'); } });
        $.ajax({
            type: "POST",
            url: "/bulkaction",
            data: "=" + action + "&ids=" + ids + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function(data){
                $('#tableHolder').html(data);
                /* bring back all functionality */
                initTable();
                /* set default sorting by date desc */
                $('th').removeClass('sortUp sortDown');
                $('th:eq(1)').addClass('sortDown'); 
                /* disable all actions */
                $('#tableActions li a').addClass('disabled');

            }
        });
    }
    return false;
});

Meine Verarbeitungslogik in der Steuerung sieht so aus

    def bulkaction
            if request.post?
                ids = params[:ids]
                #Need to create a function here to parse out my string
                puts ids #for testing purposes, just put my ids onto the console
            end

puts "This function was accessed and ran."
end

Und schließlich sagt die Konsole

Processing UserController#bulkaction (for ::ffff:xx.xxx.xxx.xxx at 2009-07-06 23                                                                             :29:49) [POST]
  Parameters: {"ids"=>"Purchases 10040963"}

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticit                                                                             yToken):
  /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  /usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start'
  /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  /usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'
  /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `each'
  /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
  /usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
  /usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'

Es wäre sehr hilfreich, wenn mir jemand sagen könnte, wo ich falsch liege.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage