--You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.To post to this group, send email to rubyonra...@googlegroups.com.To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
There are a couple gems and projects that do this:
I think this is likely outside the scope of rails core, given that most people doing ajax requests will be using the jquery-ujs or prototype-ujs helpers.
One of the issues why I would personally not use this is because, in order for it to work, you need to populate your JS with a list of all available routes throughout your app. For my own apps, I'd rather not have a list of all the applications' routes in plain text ready to be exploited.
Sure, everything is locked-down and access-controlled, but I still don't necessarily want some random visitor knowing that registered group supervisors moderate their group's content via this URL, and approve members via that URL. And again, all of this could be handled by only serving specific JS URL helpers to specific users, but it ends up creating more work than it saves.
Are there any reasons why Rails doesn't have any route helpers available for the JS/CS assets?
We're doing more and more client-side code and it is very likely that you'll need to do something like "$.post products_path, params" somewhere in your code.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com.
tl;dr;Hook into routes.rb while compiling CoffeeScript to Javascript and enable native feeling routes in CoffeeScript.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.
tl;dr;Hook into routes.rb while compiling CoffeeScript to Javascript and enable native feeling routes in CoffeeScript.
I think the problem there is that you wouldn't know to put "1" in there at asset compile time. It'd only work for un-nested collection paths like "/products".
tl;dr;Hook into routes.rb while compiling CoffeeScript to Javascript and enable native feeling routes in CoffeeScript.I think the problem there is that you wouldn't know to put "1" in there at asset compile time. It'd only work for un-nested collection paths like "/products".
On 1 June 2012 17:10, Steve Schwartz <st...@alfajango.com> wrote:
tl;dr;Hook into routes.rb while compiling CoffeeScript to Javascript and enable native feeling routes in CoffeeScript.
I think the problem there is that you wouldn't know to put "1" in there at asset compile time. It'd only work for un-nested collection paths like "/products".
But in the CoffeeScript, the developer will know the id of the resource they're calling.
So, the CS:$.get project_path(project.id)
...could be pre-processed, using a lookup in routes.rb, into:$.get "projects/#{project.id}"
...which then gets compiled to:$.get "projects/" + project.id
And voila, you have a working solution. Am I missing something? :)
Em 01-06-2012 13:20, Jeremy Walker escreveu
But in the CoffeeScript, the developer will know the id of the resource they're calling.
So, the CS:$.get project_path(project.id)
...could be pre-processed, using a lookup in routes.rb, into:$.get "projects/#{project.id}"
...which then gets compiled to:$.get "projects/" + project.id
And voila, you have a working solution. Am I missing something? :)
Yes, the id will be inside a JS variable. So if you have some constraints like /\d+/, it won't be able to check them...
tl;dr;Hook into routes.rb while compiling CoffeeScript to Javascript and enable native feeling routes in CoffeeScript.I think the problem there is that you wouldn't know to put "1" in there at asset compile time. It'd only work for un-nested collection paths like "/products".But in the CoffeeScript, the developer will know the id of the resource they're calling.So, the CS:$.get project_path(project.id)...could be pre-processed, using a lookup in routes.rb, into:$.get "projects/#{project.id}"...which then gets compiled to:$.get "projects/" + project.idAnd voila, you have a working solution. Am I missing something? :)
Yes, the id will be inside a JS variable. So if you have some constraints like /\d+/, it won't be able to check them...
--
tl;dr;Hook into routes.rb while compiling CoffeeScript to Javascript and enable native feeling routes in CoffeeScript.
I think the problem there is that you wouldn't know to put "1" in there at asset compile time. It'd only work for un-nested collection paths like "/products".
But in the CoffeeScript, the developer will know the id of the resource they're calling.
So, the CS:$.get project_path(project.id)
...could be pre-processed, using a lookup in routes.rb, into:$.get "projects/#{project.id}"
...which then gets compiled to:$.get "projects/" + project.id
And voila, you have a working solution. Am I missing something? :)
Yes, the id will be inside a JS variable. So if you have some constraints like /\d+/, it won't be able to check them...
I'm thinking aloud here, so bare with me.
You'd also need to be able to create coffeescript functionality for static [1] and dynamic segments [2], defining defaults [3], and using constraints of all types (segment constraints, request-based constraints, etc) like Rodrigo mentioned.
Though I'm not entirely convinced constraints would need to be handled, as in you'd get "route not found" on the response end if you didn't obey the constraint. The reason you'd need constraint checking on the request side (in the JS) would be so that it knows to select the next matching route down the list, which could possibly be considered an edge case to handle later. Maybe the other points I mentioned fall into the same category.
I think the point is, if you make route helpers available in coffeescript, people are going to expect *all* of the functionality of routes to work. Now I'm not intimately familiar with the innards of rails route compiling; maybe the fact that we're hooking into routes after they've already been generated by Rails makes these points easier to address.
But yes, I would think given enough time and code, you probably could make route helpers work in coffeescript at compile time. Also keep in mind, that this coffeescript functionality would need to be maintained and updated along side the core routes functionality.
The way I handle routes in my apps is to put the URLs in a data- attribute on the element being clicked or submitted (so I can use the core route helpers). Then, in my JS, I'd do:
$project = $('#project-link')$.get ( $project.data('url') )
Em 01-06-2012 13:45, Jeremy Walker escreveu:Yeah, that would work.Good point. Am I correct in saying that the only time this is a real issue is if someone defines two routes, with the same name, but different paths, e.g.
match '/products/:id' => 'products#show', :constraints => {:id => /\d/}, as: "product"match '/products/:id/extra' => 'products#show', as: "product"
In other situations, the generated path will be the same, and the server will still check the constraint if/when the request is sent, so the developer loses a little compile-time type-safety, but everything stays safe/secure.
Obviously, the two different routes with the same name thing is an issue, but that idiom strikes me as a bit odd/risky in the first place.
I just thought you were suggesting to use produc_path(id) for implementing this feature. In that case, it would raise an exception if id is not a number.
How are you thinking about the implementation of such feature while processing CS?
Also, what if someone still wants to keep using JS instead of CS? How would it be possible for them to take advantage of the asset pipeline route helpers?
On 1 June 2012 18:23, Rodrigo Rosenfeld Rosas <rr.r...@gmail.com> wrote:
Em 01-06-2012 13:45, Jeremy Walker escreveu:Yeah, that would work.Good point. Am I correct in saying that the only time this is a real issue is if someone defines two routes, with the same name, but different paths, e.g.
match '/products/:id' => 'products#show', :constraints => {:id => /\d/}, as: "product"match '/products/:id/extra' => 'products#show', as: "product"
In other situations, the generated path will be the same, and the server will still check the constraint if/when the request is sent, so the developer loses a little compile-time type-safety, but everything stays safe/secure.
Obviously, the two different routes with the same name thing is an issue, but that idiom strikes me as a bit odd/risky in the first place.
I just thought you were suggesting to use produc_path(id) for implementing this feature. In that case, it would raise an exception if id is not a number.
How are you thinking about the implementation of such feature while processing CS?
Also, what if someone still wants to keep using JS instead of CS? How would it be possible for them to take advantage of the asset pipeline route helpers?
I honestly don't know enough about CS internals to specifically say how I'd implement it. However, the two methods I'd initially explore would be:
1) Monkey-patch the compiler to recognise the routes' names as keywords/methods and then either process them for more CS compilation, or process them straight to JS. This method depends a lot on how the compiler has been written and how extendable it is. I would imagine that recognising new nodes as it parses is pretty standard, but it's how easy it is to insert that functionality in. I imagine 30mins of browsing through code would tell you if it was feasible.2) Pre-processing the CS to look for the method names by regex and then convert them to CS, before the processing starts. This method is a bit more brute-force and lots more error prone. I can think of lots of stuff that would go wrong. I'd consider it a last-ditch idea in case the CS compiler wasn't easily monkey-patchable.
With regards to JS instead of CS. My initial reaction is, they wouldn't be able to. As Rails now supports CS out of the box (it's in the generated Gemfile), then I'd expect people to be ok with technologies being built into that. If you want to not use the default Rails JS library (CoffeeScript), then you don't get all the features. However, if people felt it needed to be build in, then you could use either methods 1 or 2, using a JS interpreter for method 1.
If this is a solution that the Core team feel is worth exploring further, then I'm happy to set some time aside to seriously explore it more. Saying that, there are lots of people who know the internals of CoffeeScript and could probably verify how easy it will be to do without much effort.
Em 01-06-2012 14:42, Jeremy Walker escreveu:
On 1 June 2012 18:23, Rodrigo Rosenfeld Rosas <rr.r...@gmail.com> wrote:
Em 01-06-2012 13:45, Jeremy Walker escreveu:Yeah, that would work.Good point. Am I correct in saying that the only time this is a real issue is if someone defines two routes, with the same name, but different paths, e.g.
match '/products/:id' => 'products#show', :constraints => {:id => /\d/}, as: "product"match '/products/:id/extra' => 'products#show', as: "product"
In other situations, the generated path will be the same, and the server will still check the constraint if/when the request is sent, so the developer loses a little compile-time type-safety, but everything stays safe/secure.
Obviously, the two different routes with the same name thing is an issue, but that idiom strikes me as a bit odd/risky in the first place.
I just thought you were suggesting to use produc_path(id) for implementing this feature. In that case, it would raise an exception if id is not a number.
How are you thinking about the implementation of such feature while processing CS?
Also, what if someone still wants to keep using JS instead of CS? How would it be possible for them to take advantage of the asset pipeline route helpers?
I honestly don't know enough about CS internals to specifically say how I'd implement it. However, the two methods I'd initially explore would be:
1) Monkey-patch the compiler to recognise the routes' names as keywords/methods and then either process them for more CS compilation, or process them straight to JS. This method depends a lot on how the compiler has been written and how extendable it is. I would imagine that recognising new nodes as it parses is pretty standard, but it's how easy it is to insert that functionality in. I imagine 30mins of browsing through code would tell you if it was feasible.2) Pre-processing the CS to look for the method names by regex and then convert them to CS, before the processing starts. This method is a bit more brute-force and lots more error prone. I can think of lots of stuff that would go wrong. I'd consider it a last-ditch idea in case the CS compiler wasn't easily monkey-patchable.
Sorry, but that was not my concern when I asked you about the implementation.
I'd like to know how you think this should be implemented in the Rails side.
Given that the CS compiler detected "product_path(id)", how should it proceed to replace it with JS or CS code?
Ok, give me an evening to read the internals of routing and I'll come back to you with some suggestions/code.
Em 01-06-2012 14:42, Jeremy Walker escreveu:
On 1 June 2012 18:23, Rodrigo Rosenfeld Rosas <rr.r...@gmail.com> wrote:
Em 01-06-2012 13:45, Jeremy Walker escreveu:Yeah, that would work.Good point. Am I correct in saying that the only time this is a real issue is if someone defines two routes, with the same name, but different paths, e.g.
match '/products/:id' => 'products#show', :constraints => {:id => /\d/}, as: "product"match '/products/:id/extra' => 'products#show', as: "product"
In other situations, the generated path will be the same, and the server will still check the constraint if/when the request is sent, so the developer loses a little compile-time type-safety, but everything stays safe/secure.
Obviously, the two different routes with the same name thing is an issue, but that idiom strikes me as a bit odd/risky in the first place.
I just thought you were suggesting to use produc_path(id) for implementing this feature. In that case, it would raise an exception if id is not a number.
How are you thinking about the implementation of such feature while processing CS?
Also, what if someone still wants to keep using JS instead of CS? How would it be possible for them to take advantage of the asset pipeline route helpers?
I honestly don't know enough about CS internals to specifically say how I'd implement it. However, the two methods I'd initially explore would be:
1) Monkey-patch the compiler to recognise the routes' names as keywords/methods and then either process them for more CS compilation, or process them straight to JS. This method depends a lot on how the compiler has been written and how extendable it is. I would imagine that recognising new nodes as it parses is pretty standard, but it's how easy it is to insert that functionality in. I imagine 30mins of browsing through code would tell you if it was feasible.2) Pre-processing the CS to look for the method names by regex and then convert them to CS, before the processing starts. This method is a bit more brute-force and lots more error prone. I can think of lots of stuff that would go wrong. I'd consider it a last-ditch idea in case the CS compiler wasn't easily monkey-patchable.
Sorry, but that was not my concern when I asked you about the implementation.
I'd like to know how you think this should be implemented in the Rails side.
Given that the CS compiler detected "product_path(id)", how should it proceed to replace it with JS or CS code?
With regards to JS instead of CS. My initial reaction is, they wouldn't be able to. As Rails now supports CS out of the box (it's in the generated Gemfile), then I'd expect people to be ok with technologies being built into that. If you want to not use the default Rails JS library (CoffeeScript), then you don't get all the features. However, if people felt it needed to be build in, then you could use either methods 1 or 2, using a JS interpreter for method 1.
I'm not sure. I don't write JS anymore myself, but I can understand those who don't want or feel the need to use CS. Also, someone could want to edit some existent JS to change some fixed path by one generated by the route helpers.
An option for them would be to create a routes.js.coffee file just for storing the generated paths in some variables and require this file before any other JS using those paths...
If this is a solution that the Core team feel is worth exploring further, then I'm happy to set some time aside to seriously explore it more. Saying that, there are lots of people who know the internals of CoffeeScript and could probably verify how easy it will be to do without much effort.
I'd rather discuss the Rails-side implementation effort first, as I guess this could be the hardest part instead of the CS integration one...
--
No pressure at all :) I'm using a patched version of js-routes right now:
https://github.com/railsware/js-routes/pull/46
I'd certainly appreciate your effort on that, but I'd just like to advice you on thinking about the solution in the Rails side first as I guess this is the hardest part.
You can take a look at the js-routes source code to have some ideas though.