javascriptruby-on-railsrubyruby-on-rails-4js-routes

js-routes adding (::format) to urls


I have a simple rails project that I've been playing around in with reactjs. To add some basic navigation, I brought the js-routes library in and it works great for urls that have a path parameter such as "localhost:3000/addresses/1".

The problem I am facing is I am trying to call a "new" resource method and it adds the (::format) literally to the url which of course bombs as localhost:3000/addresses/new(.:format) is an invalid path.

I reference the "new_address_path" path as specified in the routes-js docs. The rake output for this url is below:

new_address_path    GET /addresses/new(.:format)    addresses#new

The HTML snippet utilizing the above path looks like this:

<a href={Routes.new_address_path}>Create am address</a>

ENV:

-Ruby: 2.2.4
-Rails: 4.2.6
-js-routes: 1.2.8

Route in question:

resources :addresses

What am I missing here? It seems like it is not interpreting the rails route file properly.


Solution

  • Sorry I thought I posted my solution in here.

    The problem wasn't js-routes but rather my AJAX call, I was setting the content type to JSON and I assumed that it was converting my object to JSON using the built in methods. This is not true, you need to manually convert the object to JSON via JSON.stringify(obj).

    Old ajax call:

    ....
    url: Routes.feedback_path(),
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
    data: obj,
    ....
    

    New ajax call:

    ....
    url: Routes.feedback_path(),
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
    data: JSON.stringify(obj),
    ....