Rails的身份验证令牌和Ajax [英] Rails Auth Token and Ajax

查看:149
本文介绍了Rails的身份验证令牌和Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以从我读过的其他网站和堆栈溢出,Rails的抛出此认证令牌错误,因为我的表没有通过令牌 - 这是一个安全功能。这个我明白了。

Okay so from what I've read on other websites and on stack overflow, Rails throws this Authentication token error because my form doesn't pass the token -- and it's a security feature. This I understand.

不过,我真的没有一种形式。我有阿贾克斯在这里 - 我的javascript职位id'ed信息到处理功能

However I don't really have a form. I have ajax here -- my javascript posts the id'ed information into a processing function.

所以我的问题是,如何获得身份验证令牌,以我的控制器?

So my question is, how do I get the authentication token to my controller?

我的看法是这样的:

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

相应的AJAX如下:

The corresponding ajax is as follows:

/* 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;
});

在控制我的处理逻辑看起来像

My processing logic in the controller looks like

    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

最后控制台说:

And finally the console says

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'

这将是非常有益的,如果有人能告诉我,我要去哪里错了。

It would be very helpful if someone were able to tell me where I was going wrong.

推荐答案

解决了!改变了阿贾克斯

Solved! changed the ajax to

data: "=" + action + "&ids=" + ids + "&authenticity_token=" + AUTH_TOKEN,

和我添加到头部的每一页

And I added to the head for each page

<%= javascript_tag "const AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %>

这篇关于Rails的身份验证令牌和Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆