2011-12-14 9 views
37

मैं backbone.js का उपयोग कर रहा हूं और यह बहुत अच्छा काम करता है। लेकिन जो जावा मैं जावास्क्रिप्ट टेम्पलेट के रूप में बना रहा हूं, वह रेल सीएसआरएफ सुरक्षा टोकन की कमी है। मैं इसे जावास्क्रिप्ट में बना रहे टेम्पलेट्स में कैसे जोड़ूं?रेल - जावास्क्रिप्ट में बनाए गए फॉर्मों में सीएसआरएफ संरक्षण कैसे जोड़ें?

उत्तर

26

आप कहीं अपने लेआउट में <%= csrf_meta_tag %> है और वह जे एस से आपकी पहुंच में है, तो आप इसे $('meta[name="csrf-token"]')

का उपयोग कर कैसे CSRF समर्थन में प्रत्येक रीढ़ अनुरोध

को हैक करने पर एक विचार के लिए http://eunikorn.blogspot.com/2011/07/working-with-backbonejs-in-harmony-with.html देखें उपयोग कर सकते हैं
+2

eunikorn अब एक टूटा हुआ लिंक – Crisfole

+1

https://gist.github.com/3482636 देखें कि इसमें कैसे हैक किया जा सकता है। मुझे नहीं पता कि यह वही है या नहीं। – Crisfole

+0

हाँ अवधारणा वही है। –

4

आप 'पोस्ट' या 'डिलीट' का उपयोग करने वाले प्रत्येक रूप में सीएसआरएफ टोकन प्रीपेड कर सकते हैं। यहाँ यह 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

'टोकन' कहां घोषित किया गया है? – juliangonzalez

+0

क्या @suga_shane लिखने का मतलब है, "सुनिश्चित करें कि आपके पास' <% = csrf_meta_tags%> '" है, यह है कि यह रेल सहायक आपके लिए एक टोकन उत्पन्न करता है और इसे HTML 'head' में सम्मिलित करता है। टोकन '' नाम ''csrf-token' नाम के साथ' सामग्री 'विशेषता है। – sameers

48

सबसे अच्छा तरीका है मैं इस हल, प्रपत्र के अंदर:

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

यह काम करता है, लेकिन क्यों? ['Form_authenticity_token'] नहीं है (http://apidock.com/rails/ActionController/RequestForgeryProtection/form_authenticity_token) नियंत्रक में निजी है? –

+0

यह मेरे लिए रेल 4.2.2 काम नहीं करता है। मुझे मिलता है: 'अनदेखा स्थानीय चर या विधि' form_authenticity_token '# <# <कक्षा: 0x007ff80b1d36d8> 0x007ff7eec39b58> ' – juliangonzalez

+0

अरे दोस्तों,' form_authenticity_token' जैसा दिखता है फ्रैंकलिन जैसे नियंत्रकों के लिए निजी है। एक सुझाव के रूप में मैंने जो देखा वह एक नियंत्रक '@form_token = form_authenticity_token' में एक चर घोषित किया गया था और इसे दृश्य में उपयोग करें। – lucianosousa

0

रेल के रूप में 4.2.2 आप

<%= hidden_field_tag :authenticity_token, form_authenticity_token %> 
अपने .js.erb संपत्ति फ़ाइल से

उपयोग करने के लिए अनुमति नहीं है।

हालांकि आप .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 %> 
संबंधित मुद्दे