This only works in Rails console. Not in ERB views.

Steve, while crossroads.js is unrelated to this request, thank you very much for pointing me out js-routes.

I'll give it a try soon.

Cheers,
Rodrigo.

Em 31-05-2012 17:33, Pedro Nascimento escreveu:
Try `app.root_path`.

On Thu, May 31, 2012 at 5:19 PM, Rodrigo Rosenfeld Rosas <[email protected] <mailto:[email protected]>> wrote:

    Em 31-05-2012 17 <tel:31-05-2012%2017>:11, Steve Schwartz escreveu:
    There are a couple gems and projects that do this:

    * https://github.com/railsware/js-routes
    * http://millermedeiros.github.com/crossroads.js/

    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.

    I don't understand how is jquery-ujs related to this issue.


    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.

    I agree. I'm not suggesting that all routes should be exported,
    but I'd like to be able to easily decide which ones to export.


    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.

    I'm not sure if you have a proper idea on how much saving is that...

    Even if the idea of exporting the routes to a JavaScript files is
    not accepted, at least it should be easier to use the routes from
    an ERB asset.

    "Rails.application.routes.url_helpers.send(:products_path)" is
    almost the same as an unsupported operation...

    Kind regards,
    Rodrigo.



    On Thursday, May 31, 2012 at 9:10 AM, 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 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.

Reply via email to