samedi 1 août 2015

current params and AJAX in rails

In my project it needs to add countries, regions, cities. Previously treated several cities and all. Each city had a link of this kind:

link_to "Moscow", params.merge(city_name: "moscow")

and I get

www.site.com/moscow. (when params = {"controller"=>"index", "action"=>"index", "city_name"=>"moscow"})

But now I'm using Ajax to select a city from the regions (states). Сorny, send state_id - I get a list of cities in the state.

But after AJAX - sending data to the controller the current params changed. (before: {"controller"=>"index", "action"=>"index"}, after: {"action"=>"get_list_cities", "controller"=>"states", "id"=>"4315"}). And my construction with params.merge(city_name: "moscow") does not work any more.

Is it possible to roll back both params or work with Ajax, without changing the params?

I understand that it is possible to use another method of formation of links to each city, but this a way like .merge is very convenient and concise for me.

UPDATE:

url_for(params.clear.merge(Rails.application.routes.recognize_path(request.referer))) - don't work !

UPDATE 2:

link_to area.name, "#", remote: true,  data: {toggle: "tab", ajax_path: get_list_cities_path(area), value: "#{area.id}"}

def get_list_cities
    @cities = Area.find(params[:id]).cities.where(status: true)
    respond_to do |format|
      format.js
    end
   end

$('ul.nav').on 'click', 'li > a', (e) ->
        e.preventDefault()
        $(this).parent().siblings().removeClass 'active'
        $(this).parent().addClass 'active'
        $.ajax
          url: $(this).data('ajax-path')
          type: 'GET'
          dataType: 'script'

Aucun commentaire:

Enregistrer un commentaire