Does Backbone.js have something like 'rake routes' from Ruby on Rails? - ruby-on-rails

Is there an easy way to see all the routes my Backbone.js application has built while it's running?
Ruby on Rails has rake routes, which shows things like this:
unicorns GET /unicorns(.:format) unicorns#index
POST /unicorns(.:format) unicorns#create
new_unicorn GET /unicorns/new(.:format) unicorns#new
edit_unicorn GET /unicorns/:id/edit(.:format) unicorns#edit
unicorn GET /unicorns/:id(.:format) unicorns#show
PUT /unicorns/:id(.:format) unicorns#update
DELETE /unicorns/:id(.:format) unicorns#destroy
Does Backbone have an equivalent?

You can always check a router prototype/instance routes property to get all statically declared routes. So if you're not generating them programmatically, you'll get the cleanest output as so:
console.log( router.routes );
If you're using this.route() to dynamically add routes, then you should check on the Backbone.History object, this will be the complete list of routes used inside your app; although the output is a little bit messiers (routes are compiled to regexp, etc).
console.log( Backbone.history.handlers );
Note that this property ain't documented, so there's no garantee it will be kept in future version of Backbone. I'd only use it for debugging purpose.

Related

Query which controller will be used for a URL

Question
I'm looking for something in the rails console or similar where I can call get_controller_for_path('/some/path/') and it will return the corresponding controller.
Background
I have a rails project with a lot of routes. I'm investigating a routing problem and want to confirm that a given URL will match a specific route.
I can use bundle exec rake routes to view the list of routes, but that still requires my human eyes to parse the hundreds (thousands?) of routes and figure out what's going on.
Here you go: Rails parse url to hash(Routes)
That says it's Rails.application.routes.recognize_path "/accounts/1"
You can also use assert_routing in your tests. If you don't have tests, now is a very good time to add some. And you can read the source of assert_routing, and assert_recognizes, to see what they do.

Rails "Show" routes

I'm having trouble creating routes for my Rails app and google is returning mostly results for rails 2-3 from 5 or more years ago.
I'm trying to create a "show" route without using resources, as I need multiple show views.
If I do the following :
get "/parameters/:id" => "parameters#show"
Then if I directly type in the path, like "parameters/12" it will work, but when I run rake routes it doesn't show a path next to the route. And I can't seem to get a path to work for it.
I'm sure I'm forgetting something really obvious here, but I've been at this problem for a few hours and haven't found a solution so I would really appreciate some help.
Because you are defining a custom route, Rails doesn't know how to generate the _url and _path named routes for you automatically.
In order to get the named routes generated, you need to help Rails by specifying the as argument
get '/parameters/:id', to: 'parameters#show', as: 'parameters_show'

Mixing backbone.js routers and rails controllers

I'm currently attempting to wrap my head around grafting backbone.js into my rails application. For starters, I want to build it into a a specific part of the rails application at /applications. With that said I have a rails resource "resources :applications" which gives me localhost:3000/applications. Now when I instantiate backbone for /applications I get anchor tags for the backbone routing at that rails resource. IE localhost:3000/applications/#applications/5.
Given that I'm only going to be using backbone at specific parts of the rails app, thus not making it a single page app, is this the right way to do things? The URL appears to be a bit redundant.
The correct answer might be that I need to do away with the backbone router? If so, then how can :id be passed to the backbone application when attempting to look up a collection / model.
The point of me using backbone is to help organize a particular section of the rails app that will be javascript heavy.
I should mention that I can change the router to something like:
routes:
'': 'index'
':id': 'show'
which will give me the url of localhost:3000/applications/#/1 - however I think this paints me into a corner and will not allow me to use backbone on other rails resources. If I were to have localhost:3000/dashboard with backbone invoked then the wrong backbone.js functionality would get executed.
Another thought would be to have an invocation of a backbone router per rails resource. I could use the aforementioned routes code since the router would only be invoked for that rails resource.
I ended up figuring this out. I switched to using backbone-rails and followed their tutorial in getting a sample application up and running; https://github.com/codebrew/backbone-rails. The solution came in the rails html.erb and only loading the specific router that I needed for the rails resource.
routes:
"new" : "newPost"
"index" : "index"
":id/edit" : "edit"
":id" : "show"
".*" : "index"
Then in say a posts.html.erb I could put the following.
<div id="posts"></div>
<script type="text/javascript">
$(function() {
// Blog is the app name
window.router = new Testing.Routers.PostsRouter({posts: <%= #posts.to_json.html_safe -%>});
Backbone.history.start();
});
</script>
If my answer doesnt make sense, I suggest going through the tutorial on the github link that was aforementioned.

Generating an object's absolute url without html markup

Is there a method like "full_url" such that #comment.full_url or full_url_for(#comment) returns "http://www.host.com/comments/id" where www.host.com is the default host domain and id is #comment.id. Or, if not, what would be an elegant way to generate this url string?
I'm pretty new at Rails, most of the methods I've learned insert the tag and other markup.
url_for is not helping because I can't do something like the following:
url_for(#comment, {:only_path => false})
I've spent way too much time trying to figure this out. It came down to either hacking or asking for the right way on SO. Here I am.
If you are setting up your routes correctly in your config/routes.rb file then you should have access to named routes in your controller and in your views. Which should mean that all you should need to do is:
comment_path(#comment)
Or for the full url
comment_url(#comment)
To see a list of all of the routes from the command line, you can type rake routes from the project root. Welcome to rails! Here is a good resource for rails 3 routing: http://guides.rubyonrails.org/routing.html
some additional resources via Railscasts:
http://railscasts.com/episodes/231-routing-walkthrough
http://railscasts.com/episodes/232-routing-walkthrough-part-2

Rails: how do you access RESTful helpers?

I'm trying to work through this guide to Rails routing, but I got stuck in section 3.3:
Creating a RESTful route will also make available a pile of helpers within your application
and then they list some helpers like photos_url, photos_path, etc.
My questions:
Where can I find the complete list of helpers that is "made available?"
Is there a way to call the helpers in the console? I created an app, then opened up the console with script/console. I tried to call one of the helpers on the console like this:
>> entries_url
But got:
NameError: undefined local variable or method `entries_url' for #<Object:0x349a4>
from (irb):8
You have several questions in there, most of which have already been answered by people below.
The answer to one that wasn't fully addressed however, is: yes you can use the script/console to see where your routes go. Just type in app.[route_helper] and it will respond with the path. For example app.users_path will return /users/
So for your example type app.entries_url for the full URL - or app.entries_path for its relative path within the console.
rake routes at the command line should get you that list.
I think this may be what you are looking for ... http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf
You can access other helpers in the console by prepending "helper."; ie. helper.progress_box (assuming #progress_box exists of course)
From memory, you can't call url/path helpers from the console for some reason.

Resources