Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:58:32+00:00 2026-06-06T11:58:32+00:00

I know rails provided CSRF protection by default from rails3. but my web application

  • 0

I know rails provided CSRF protection by default from rails3.
but my web application is a single page application, and all communication depend on ajax.

So, how can I get CSRF token from server before each ajax calling?
or what I can do is just take off CSRF protection, right?

NOTE: we don’t use any rails view to produce the web page, even the homepage, so the solution to put “<%= csrf_meta_tag %> ” in .html or .erb is invalid., we use javascript framework within one page.

my env: rails3.2.3, devise2.0.4, dojo1.7.2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:58:34+00:00Added an answer on June 6, 2026 at 11:58 am

    I would advice you to take a look at the Rails-backbone project. Specifically the backbone_rails_sync.js file.

    It shows how this gem sends the CSRF-token data along with any ajax request you make to the server.

    Also, here are some docs by the Django project that give you an example of how to override the default jQuery ajax() method to always send a CSRF token.

    updated based on comment:

    So what you where asking is how to implement CSRF when using nothing but a REST API and no views/forms are used in any way.

    The answer is: don’t include CSRF protection.

    Cross-site request forgery is a way where another (malicious) web site reproduces or imitates a request and poses as a valid user while sending invalid data to the server.

    This is only a problem when a user needs to authenticate and the authentication state is saved in the cache (to persist between page views). If a malicious web site hijacks this cache, it can submit data while pretending to be this user, without the user and server ever noticing this. This is what CSRF protection is hired to do, it injects a special string into the form that is only valid for this particular request and is unique for each other request, so even if the session is hijacked, the CSRF protection prevents any modification on the server.

    In your case however, you are using HTTP API calls, and thus have to send your authentication every request through the use of the Authorization header. This way, the credentials are never cached and can’t be hijacked, so there is no need for CSRF protection.

    There are other ways however to make sure your RESTful API is secure:

    • Always send data over the wire in an encrypted form (https/ssl)
    • Use a good authentication method like oAuth(2), API tokens or Basic HTTP Authentication (the last two are less secure, but still secure enough for smaller applications)
    • Always check every value going into your application before acting on them
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I'm a newbie in Rails but this is the second day I'm
Does anybody know how to create a rails application that use noSql database? We
Do the major web application frameworks (Rails, Django, etc) have libraries that provide functionality
I know that an after_commit callback is provided for ActiveRecord objects in Rails 3.
I'm building a Rails 3 application that I plan on provided as a subscription-based
I know ruby (on rails) uses lots of magic, but I use PHP, is
I'd like to implement a scalable chatroom in rails using AJAX. I know from
OK, I'm using a JSON-enabled Rails web-service to provide data to an iPhone application.
I know new rails apps come with an empty favicon.ico file. I want to
I know that RAILS_ROOT is the old way of getting Rails.root and that it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.