Pedro Souza
Pedro Souza

Reputation: 337

Url in Ruby/Rails

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'

Upvotes: 1

Views: 344

Answers (3)

deefour
deefour

Reputation: 35370

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).

Upvotes: 1

deefour
deefour

Reputation: 35370

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 %>,
// ...

Upvotes: 3

Chris Clower
Chris Clower

Reputation: 5104

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.

Upvotes: 0

Related Questions