2011-12-14 18 views

答えて

26

あなたがどこかに自分のレイアウトで<%= csrf_meta_tag %>を持っており、それはJSからあなたにアクセス可能である場合は、各バックボーン要求

にCSRFのサポートにハックする方法についてのアイデアのため http://eunikorn.blogspot.com/2011/07/working-with-backbonejs-in-harmony-with.htmlを参照してください $('meta[name="csrf-token"]')

を使用してアクセスすることができます

+2

eunikorn今は壊れたリンクになっています – Crisfole

+1

これがどのようにハッキングできるかの別の例はhttps://gist.github.com/3482636を参照してください。それが同じかどうかはわかりません。 – Crisfole

+0

ああ、概念は同じです。 –

4

「投稿」または「削除」を使用するすべてのフォームにcsrfトークンを付加することができます。ここでは、CoffeeScriptのである:

$ -> 
    for f in $("form") 
    if f.method == 'post' or f.method == 'delete' 
     $(f).prepend("<input type='hidden' name='authenticity_token' value='" + token + "'>") 

あなたのレイアウトで<% = csrf_meta_tags%>があることを確認してください。すでに標準の「アプリケーション」レイアウトにあるはずですが、別のレイアウトを使用している場合は追加してください。

+2

'token'はどこで宣言されていますか? – juliangonzalez

+0

@suga_shaneは、 "<%= csrf_meta_tags%>' "を持っていることを意味します。これはRailsヘルパーがトークンを生成してHTMLの' head'に挿入することを意味します。トークンは ''タグの 'csrf-token'という名前の' content'属性です。 – sameers

48

ベストな方法は、私は、フォーム内で、これを解決:あなたの.js.erb資産ファイルから

<%= hidden_field_tag :authenticity_token, form_authenticity_token %> 

を使用することはできません4.2.2

<%= hidden_field_tag :authenticity_token, form_authenticity_token %> 
+0

それは動作しますが、なぜですか?コントローラ内の['form_authenticity_token'](http://apidock.com/rails/ActionController/RequestForgeryProtection/form_authenticity_token)はプライベートではありませんか? –

+0

これは私にとってRails 4.2.2ではうまくいきません。私は: '未定義のローカル変数またはメソッド 'form_authenticity_token for#<#:0x007ff7eec39b58>' – juliangonzalez

+0

のようになりました。ちょっと、' form_authenticity_token'のように見えるフランクリンのようなコントローラのプライベートです。私が提案として見たのは、コントローラの変数 '@form_token = form_authenticity_token'を宣言し、それをビューで使用することでした。 – lucianosousa

0

Railsのためのよう。

.js.erbファイル内にフォームを作成することができます。フォームに.html.erbというファイルを含むビューでは、hidden_field_tagヘルパーを使用してトークン要素を生成します。この要素はフォーム外で生成されるため、この要素をフォームに追加するためにjqueryを使用できます。研究の

ケース:SweetAlert(最初のバージョン、バージョンがあまりにもこの問題を解決しているようだ)

show.js.erb

$('.js-button-apply-offer').click(function(e) { 
var urlOffer = $(this).attr('data-url-offer'); 
var modalParams = { 
    type: 'warning', 
    title: 'add file', 
    text: '<p>Need to add a file before continuing</p>' // This is a hack for Sweet alert, solved in SweetAlert2 Consider upgrade 
    +"<form action='"+urlOffer+"' id='formCustomCV' method='post' enctype='multipart/form-data' data-remote='true'>" 
    + "<input type='file' name='custom_cv' id='fileToUploadAlert' accept='application/pdf'>\n" 
    +"</form>", 
    html: true, 
    showCancelButton: true, 
    confirmButtonColor: '#DD6B55', 
    confirmButtonText: 'Send', 
    cancelButtonText: 'Cancel', 
    closeOnConfirm: false 
    } 
swal(modalParams, 
function(){ 
    var form_token = $('#form_token'); 
    $('#formCustomCV').append(form_token).submit(); //update to submit using ajax 
}); 

show.html.erb

<%= button_tag t('offers.offer.apply'), 
    class: 'center-block btn btn-success js-button-apply-offer', 
    id: "js-button-apply-offer", 
    data: { 
    url_offer: apply_talents_offer_path(@offer), 
    } 
%> 
<%= hidden_field_tag :authenticity_token, form_authenticity_token, id: :form_token %> 
関連する問題