JavaScript Rails Defaults to “text/javascript” Content Type in Response to Unobtrusive JavaScript Form Built With form_for rails default text,rails textarea default value,rails text default

I'm using Rails 4.1.1 and Ruby 2.1.2p95. I am using Slim as my HTML templating language (similar to HAML). I'm using Thin as my Rack server.

I am trying to make a fully-AJAX application (one of my requirements is that I may not reload the page).

PROBLEM

I have a user sign-up page that looks like this:

= form_for( User.new, url: user_path, remote: true ) do |f|
  = f.label :username
  = f.text_field :username, label: true
  = f.label :password
  = f.password_field :password, label: true
  = f.label :password_confirmation
  = f.password_field :password_confirmation, label: true
  = f.submit

I have a UsersController action that looks like this (for the purposes of this example):

  def create
    render html: "PRETEND A USER WAS CREATED"
  end

I have a response handler that looks like this (this is not important really - suffice to say the success handler does not fire and the error handler does):

   $("#dynamic_body").on( "ajax:success", "[data-remote]", function( event, data, status, xhr ) {
      $("#dynamic_body").empty();
      $("#dynamic_body").append( data );
   });
   $("#dynamic_body").on( "ajax:error", "[data-remote]", function( event, xhr, status, error ) {
      $("#dynamic_body").empty();
      $("#dynamic_body").append( error );
   });

Rails sends back a response with a Content-Type header set to "text/javascript". Therefore the browser tries to parse the response as JavaScript, which of course fails, and causes the error event instead of the success event for the AJAX response.

SOLUTIONS THAT I DON'T LIKE

I can fix this by being explicit with Rails, either by specifying the request URL format in the form_for call:

= form_for( User.new, url: user_path( format: :html ), remote: true ) do |f|
  ...

or by specifying the header explicitly in the controller action:

def create
    render html: "PRETEND A USER WAS CREATED", content_type: :html
end

but I'm pretty upset that I even have to do this. Nobody asked Rails to return a javascript response. Their own documentation says that the controller defaults to "text/html", which is obviously true most of the time but not when using their unobtrusive javascript tools.

I would love to understand what is going on here and if there are any ways I can configure my application so I don't need to write these extra workarounds.

Thanks in advance good people.

Answer:1

I want to access the refs from the render function, and set it to the state. Here is my code: export default class App extends Component { constructor(props) { super(); ...

I want to access the refs from the render function, and set it to the state. Here is my code: export default class App extends Component { constructor(props) { super(); ...

Someone told me that there's !< and !> in JavaScript. I tried it but it didn't work. Is there something like this in JavaScript? If not in JavaScript, is it there in any other language?

Someone told me that there's !< and !> in JavaScript. I tried it but it didn't work. Is there something like this in JavaScript? If not in JavaScript, is it there in any other language?

  1. javascript there was an error processing the request
  2. is-there javascript
  3. in javascript there are objects of

I am using the Plugins Jquery Dropdown and ListJS. The jQuery Dropdown Plugin offers a hide event: $('.dropdown').on('hide', function(event, dropdownData) { }); In this function I am using the ...

I am using the Plugins Jquery Dropdown and ListJS. The jQuery Dropdown Plugin offers a hide event: $('.dropdown').on('hide', function(event, dropdownData) { }); In this function I am using the ...

  1. working with objects in python
  2. working with objects in powershell
  3. working with objects in java
  4. working with objects in typescript
  5. working with objects in c#
  6. working with objects lesson four
  7. working with objects in php
  8. working with objects in ms word
  9. working with objects in coreldraw
  10. working with objects in react
  11. working with objects in powerpoint
  12. working with objects in illustrator

I was doing the Google's XSS game (https://xss-game.appspot.com/level4) and I managed to solve the 4th level. I didn't completely undestand how, though. I don't understand why if I inject the encoding ...

I was doing the Google's XSS game (https://xss-game.appspot.com/level4) and I managed to solve the 4th level. I didn't completely undestand how, though. I don't understand why if I inject the encoding ...