Search code examples
ruby-on-railsw3m

Is it possible to disable JS in RoR project?


New to rails. Was working on chapter 2 of railsTutorial.

I was pair programming with someone who only uses text based browser w3m. So once we created user resource, we created a new user and tried deleting it but couldn't. I could do it on my Chrome browser though, so thought this may be issue with JS or so.

How do we resolve this?


Solution

  • Rails relies on javascript for links to http DELETE actions.

    link_to "Delete Thing", thing_path(@thing), method: :delete  
    

    Generates a link with a data-method attribute. The Rails UJS driver then catches the click and turns it into into an AJAX HTTP DELETE request.

    method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If href: '#' is used and the user has JavaScript disabled clicking the link will have no effect. If you are relying on the POST behavior, you should check for it in your controller's action by using the request object's methods for post?, delete?, patch?, or put?. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

    Added:

    The whole reason we do this is that Rails is built around restful interfaces using the full range of HTTP methods; GET, POST, DELETE, PUT, PATCH. But some browsers can only send GET & POST requests and HTML does not define links which perform other actions than GET.

    If no javascript is a requirement you can create a form to send requests other than GET:

    <%= form_for @thing, method: :delete do |f| %>
       <%= f.submit 'Delete thing' %>
    <% end %>
    

    Note that this form uses method="post" for compatability. Rails adds a special hidden input to the resulting form:

    <input name="_method" type="hidden" value="delete" />
    

    Which Rails uses to fake the extended set of HTTP methods (DELETE, PUT, PATCH, etc).