Rails:make(link_to“example”,“#”,remote:true)bypass(respond_to ... format.js)

时间:2016-10-08 13:51:50

标签: javascript ruby-on-rails-5

我有以下问题:

在我的页面上,我有2个远程链接。第一个是:

[dummy=nullptr]

这是fadeToggle对象描述的简单链接:

<p><%= link_to "Description", "#", remote: true, id: "desc-link" %></p>

第二个是Ajax调用:

$(document).on "ready page:load", ->
  $("section.object").on "click", "#desc-link",  ->
    $("#object-desc").fadeToggle()

并且控制器在点击时应该执行STUFF:

<a type="button" class="btn btn-default", 
  href='/users/<%= Object.find(object_to_display).user.id %>/objects',
  data-remote="true" id='object-link'>Test</a>

我的问题是,每次当我使用第一个链接打开或关闭描述时,也会发生STUFF,这不应该是这种情况。如何在点击第一个链接时避免发生STUFF?

1 个答案:

答案 0 :(得分:0)

第一个链接不需要In [2]: import requests ...: ...: key = "my_key..." ...: ...: coord_params = {"output": "json", ...: "key": key} ...: coords_url = "https://dev.virtualearth.net/REST/v1/Locations" ...: template = "{add},US" ...: url = "https://api.phillypolice.com/jsonservice/Map/searchAddress.json" ...: with requests.Session() as s: ...: coord_params["query"] = template.format(add="425 E. Roosevelt Blvd") ...: ...: js = s.get(coords_url, params=coord_params).json() ...: latitude_longitude = str(js[u'resourceSets'][0][u'resources'][0]["po ...: int"][u'coordinates']) ...: print(latitude_longitude) ...: data = s.get(url, params={"latlng": latitude_longitude}) ...: print(data.json()) ...: [40.02735900878906, -75.1153564453125] {'response': ['35', '2', 'Marques Newsome', 'PPD.35_PSA2@phila.gov ', '267-357-1436']} 。设置会导致jquery_ujs在您单击时尝试通过ajax自动提交链接。删除remote: true会阻止切换链接触发remote: true操作。

另外,您的objects处理程序应该返回click或致电e.preventDefault()以阻止浏览器尝试关注该链接(基本上您是在告诉浏览器&#34} ;忽略这个点击,我处理了它&#34;):

false