2014-11-24 63 views
17

我试图给我的网站上的用户发送关于品牌名称的“点数”或“点数”。Rails中的422(不可处理的实体)?由于路线或控制器?

我有合适的视图看中Twitter的部件...

<p><a href="https://twitter.com/share" class="twitter-share-button" data-text="Check Out This Awesome Website Yay" data-via="BrandName" data-hashtags="ProductName">Tweet</a> 
<div id="credited"></div> 
<script>window.twttr = (function (d, s, id) { 
    var t, js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); js.id = id; 
    js.src= "https://platform.twitter.com/widgets.js"; 
    fjs.parentNode.insertBefore(js, fjs); 
    return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f) } }); 
}(document, "script", "twitter-wjs")); 
</script>  

我对JS都写了漂亮....

function creditTweet() { 
    $.post(
    "/credit_tweet", 
    {}, 
    function(result) { 
     var text; 
     if (result.status === "noop") { 
     text = "Thanks for sharing already!"; 
     } else if (result.status === "ok") { 
     text = "5 Kredit Added"; 
     } 
     $("#credited").html(text); 
    } 
); 
} 

$(function() { 
    twttr.ready(function (twttr) { 
    window.twttr.events.bind('tweet', creditTweet); 
    }); 
}); 

现在的问题是无论是在控制器OR中的路线(我发布的地方)。我认为路由很好,因为POST几乎可以工作,因为这是对维基百科上的错误的描述 - “422 Unprocessable Entity(WebDAV; RFC 4918) 请求格式正确但由于语义而无法跟踪错误“。

那么,你们看到我的控制器中的ruby代码有什么问题吗?

class SocialKreditController < ApplicationController 
    TWEET_CREDIT_AMOUNT = 5 

    def credit_tweet 
    if !signed_in? 
     render json: { status: :error } 
    elsif current_user.tweet_credited 
     Rails.logger.info "Not crediting #{ current_user.id }" 
     render json: { status: :noop } 
     else 
     Rails.logger.info "Crediting #{ current_user.id }" 
     current_user.update_attributes tweet_credited: true 
     current_user.add_points TWEET_CREDIT_AMOUNT 
     render json: { status: :ok } 
     end 
    end 
end 

而且在我的routes.rb,这是很简单的,所以我怀疑有什么错在这里...

get 'social_kredit/credit_tweet' 
    post '/credit_tweet' => 'social_kredit#credit_tweet' 

在哪里哦,这是哪里的错误?我显然不知道HTTP请求。

+0

当ActiveRecord对象无法更新/保存时,IME栏通常返回422。你的日志说什么? – ihaztehcodez 2014-11-24 05:30:35

+0

看起来像.... 开始POST“/ credit_tweet”for 127.0.0.1于2014-11-24 00:34:53 -0500 正在处理SocialKreditController#credit_tweet as */* 无法验证CSRF令牌的真实性 已完成422 1ms内无法处理的实体 ActionController :: InvalidAuthenticityToken - ActionController :: InvalidAuthenticityToken: – piratetone 2014-11-24 05:35:27

+0

您可以使用显示此错误的行从rails服务器发布日志吗?另外,为什么你有两个路线'get'和'post'指向相同的方法? – Surya 2014-11-24 05:45:12

回答

38

我明白了!

我加了一个...

skip_before_action :verify_authenticity_token 

到控制器。

检出日志并发现无法验证CSRF令牌时发现该问题。

+9

您可能还想查看[此问题](http://stackoverflow.com/questions/7203304/warning-cant-verify-csrf-token-authenticity-rails)解决方案,不需要禁用csrf令牌检查。 – ihaztehcodez 2014-11-24 05:52:11

+0

谢谢@ihaztehcodez - 此skip_before_action:verify_authenticity_token仅用于登录后用户所必需的控制器。是否有任何主要原因可能导致我想使用此链接中提供的AJAX解决方案通过此小代码段? – piratetone 2014-11-24 06:11:41

+8

@piratetone:是的,这就是安全。 – Surya 2014-11-24 06:15:05

1

我面对同样的问题。 它添加

skip_before_action后挑选出:verify_authenticity_token

在你的控制器在您的JS呼吁或发送数据的顶部。

class UserController < ApplicationController 
    skip_before_action :verify_authenticity_token 
    def create 
    end 
end 

如代码段所示。