I came to this same problem and think that it may perhaps need some convention...First my current solution to this. Suggestion to Rails Core at the bottom.
My solution came from "defining" an api subdirectory/route namespace and
that all routes under that namespace are ones to be exported to view
clients. The way I achieved this was by adding an #as_json to the
Journey::Routes object, adding a RoutesController (in case the client needs
to confirm/get updated routes at any point), plus setting a global route
object in the JS of the served HTML. (see the attached example file)
This returns an object that looks like this:
[ {:name=>"api_comments",
:path=>"/api/comments(.:format)",
:jsonp=>false, :verbs=>["GET"] },
{ :name=>"api_comment_cache",
:path=>"/api/comments/:comment_id(/:commenter_id)(.:format)",
:jsonp=>true, :verbs=>["GET"]}
]
Then I created a pared down version of the Rails router/parser on the JS
end to find the right route for the given request method and fill in the
necessary parameters.
Note I also needed to maintain which paths are called with JSONP because
they need fully qualified URLs and a wholly different calling mechanism.
At this point it is just a specific list in the #as_json method. It could
be identified by another namespace, or something.
So....what is my actual suggestion to Rails Core?
That we could consider formalizing Routes#as_json/to_xml for this type of
usage. But, quickly, you get to considering implementing the whole Rails
routes algorithm in JS for this to work in anything like an automatic
process.
But, the goal of DRY routes, with the routes.rb file being in charge is a
very appealing one.
On Thursday, 31 May 2012 09:10:28 UTC-4, Rodrigo Rosenfeld Rosas wrote:
>
> 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.
>
> Currently for doing that you'd need to create a file like
> asset.js.coffee.erb and then do something like:
>
> $.post <%= Rails.application.routes.url_helpers.send(:products_path) %>,
> params
>
> This is not only ugly and impractical, but I also don't want my files to
> be processed with ERB. This is not even possible inside my ECO templates.
>
> So I decided a while back to create a routes.js.coffee.erb to export my
> named routes in some way to my other scripts. But as I needed more and
> more features it got a lot messy now:
>
> <% h = Rails.application.routes.url_helpers
> {
> sections_json: :json, fields_from_parent: :json_id, move_field:
> :json,
> field: :json_id, field_save: :json, remove_field: :html_id,
> field_aggregates_autocomplete: [:json_id, :data_type],
> field_dependents_autocomplete: :json_id,
> field_set_aggregator: [:html_id, :aggregator_id],
> field_remove_aggregate: :html_id,
> field_add_dependent: [:html_id, :dependent_id],
> field_remove_dependent: [:html_id, :dependent_id],
> section_update: :html_id, section_create: :json,
> }.each do |named_route, options|
> options = Array(options)
> format = options.shift
> format_options = {}
> format_options[:format] = :json if format =~ /json/
> options.push format_options
> if format =~ /_id/ %>
> window.<%= named_route %>_path = (id, options)->
> path = "<%= h.send :"#{named_route}_path", '999', *options
> %>".replace('999', id)
> <% if options.size > 1 %>
> path = path.replace(k, v) for k, v of options
> path
> <% end %>
> <% else %>
> window.<%= named_route %>_path = '<%= h.send
> :"#{named_route}_path", format_options %>'
> <% end %>
> <% end %>
>
>
> See how to maintain something like this can become complicated?
>
> Couldn't Rails provide an easier built-in way for the named routes to be
> easily exported as JS functions/variables?
>
> This way, instead of each application define their own helpers for such
> a common requirement, an official way would exist and developers moving
> their jobs or projects would be aware where to look for.
>
> I just feel there is some convention missing here.
>
> Cheers,
> Rodrigo.
>
>
On Thursday, 31 May 2012 09:10:28 UTC-4, Rodrigo Rosenfeld Rosas wrote:
>
> 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.
>
> Currently for doing that you'd need to create a file like
> asset.js.coffee.erb and then do something like:
>
> $.post <%= Rails.application.routes.url_helpers.send(:products_path) %>,
> params
>
> This is not only ugly and impractical, but I also don't want my files to
> be processed with ERB. This is not even possible inside my ECO templates.
>
> So I decided a while back to create a routes.js.coffee.erb to export my
> named routes in some way to my other scripts. But as I needed more and
> more features it got a lot messy now:
>
> <% h = Rails.application.routes.url_helpers
> {
> sections_json: :json, fields_from_parent: :json_id, move_field:
> :json,
> field: :json_id, field_save: :json, remove_field: :html_id,
> field_aggregates_autocomplete: [:json_id, :data_type],
> field_dependents_autocomplete: :json_id,
> field_set_aggregator: [:html_id, :aggregator_id],
> field_remove_aggregate: :html_id,
> field_add_dependent: [:html_id, :dependent_id],
> field_remove_dependent: [:html_id, :dependent_id],
> section_update: :html_id, section_create: :json,
> }.each do |named_route, options|
> options = Array(options)
> format = options.shift
> format_options = {}
> format_options[:format] = :json if format =~ /json/
> options.push format_options
> if format =~ /_id/ %>
> window.<%= named_route %>_path = (id, options)->
> path = "<%= h.send :"#{named_route}_path", '999', *options
> %>".replace('999', id)
> <% if options.size > 1 %>
> path = path.replace(k, v) for k, v of options
> path
> <% end %>
> <% else %>
> window.<%= named_route %>_path = '<%= h.send
> :"#{named_route}_path", format_options %>'
> <% end %>
> <% end %>
>
>
> See how to maintain something like this can become complicated?
>
> Couldn't Rails provide an easier built-in way for the named routes to be
> easily exported as JS functions/variables?
>
> This way, instead of each application define their own helpers for such
> a common requirement, an official way would exist and developers moving
> their jobs or projects would be aware where to look for.
>
> I just feel there is some convention missing here.
>
> Cheers,
> Rodrigo.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-core/-/JcRpOEVShYwJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.
client_routes.rb
Description: Binary data
