1

I want to use the same AJAX call to my routes. How I set url??

routes:

put :sort, :path => 'activities/sort', :controller => 'activities'
put :sort, :path => 'articles/sort', :controller => 'articles'

JS:

$.ajax({
            type: "PUT",
            url: 'activities/sort',  # I wanna change this line
            data: {ids:newOrder}, 
            dataType: "html",
            error: function()
            {
                alert('Positions has not been saved.');
                $('#sortable').sortable('cancel');
            }
         });

I thinking something like this:

url: <%= action: 'sort' %>

But it redirect to 'localhost:3000/sort' but I want 'localhost:300/current_controller/sort'

Pedro Souza
  • 345
  • 1
  • 3
  • 14

3 Answers3

3

Your route generates the following

sort PUT      /activities/sort(.:format)      activities#sort

As mentioned in this SO question

Sprockets is evaluating the ERB outside of the context of your Rails app

So, .js.erb files don't have access by default to the URL helper methods; they must be included explicitly.

<% 
  # at the top of your file somewhere
  url = Rails.application.routes.url_helpers
%>

// ...
url: <%= url.sort_path %>,
// ...
Community
  • 1
  • 1
deefour
  • 34,974
  • 7
  • 97
  • 90
  • undefined local variable or method `sort_path' for main – Pedro Souza Aug 17 '12 at 01:16
  • This solution worked only in activities page. In articles page, the url is the same of activities page. `url.sort_path` get the first route with `sort`.. I need something to get current controller name.. `controller_name/sort` – Pedro Souza Aug 17 '12 at 01:41
  • Yeah, you need to learn to **[name your routes](http://guides.rubyonrails.org/routing.html#naming-routes)**. Add `as: :sort_activities` and `as: :sort_articles` to the proper routes in `config/routes.rb` and then reference them as `sort_activies_path` and `sort_articles_path` in your `.js.erb` files. – deefour Aug 17 '12 at 01:44
  • But in this case I will need Copy and Paste my Ajax method for each route? Wanted to use only once – Pedro Souza Aug 17 '12 at 01:48
  • Sorry, your question is pretty poorly worded. I will post how I handle this in a separate answer. – deefour Aug 17 '12 at 03:10
1

I use something like this:

def javascript_variables(variables = nil)
  @inline_js_variables ||= {}
  @inline_js_variables.merge!(variables) and return if !variables.nil?

  output = ''

  @inline_js_variables.each do |variable, value|
    output << "var #{variable} = #{value.to_json};\n"
  end

  output.strip.html_safe
end

You might put the above in app/helpers/application_helper.rb. Then in your ApplicationController, you can put this at the top of the class.

helper :all

Now, in your action you can do something like

def your_activities_action
  javascript_variables({ ajax_route_path: sort_activities_path })
end

def your_articles_action
  javascript_variables({ ajax_route_path: sort_articles_path })
end

In your app/views/layouts/application.html.erb you can put the following in the <head>

<script>
  <%= javascript_variables %>
</script>

Finally, in your .js file you can use ajax_route_path in your url parameter.

url: ajax_route_path,

Based on this answer and my other one, you should be able to piece together what you're trying to accomplish. For example, if you need both sort_activities_path and sort_articles_path included in the same action

def your_articles_action
  javascript_variables({ ajax_activities_route_path: sort_activities_path,
                         ajax_articles_route_path:   sort_articles_path })
end

and then modify your Javascript file to use each accordingly (for example, by wrapping the Ajax method in your Question in a function, accepting the route for url as an argument).

deefour
  • 34,974
  • 7
  • 97
  • 90
0

Try putting a / in front, like so:

'/activities/sort'

Otherwise, it's going to use, as you said, /current_controller/activities/sort because without the / in front, the paths are relative rather than absolute.

Chris Clower
  • 5,036
  • 2
  • 18
  • 29