gpt4 book ai didi

javascript - 同一 link_to 助手的不同行为 - 模板丢失

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:54 25 4
gpt4 key购买 nike

我在一个页面中有两个 link_to 助手,创建方式不同。我需要以 .js 格式而不是 .html 格式获得响应。下面的这个 link_to 工作正常,处理,输出 Processing by PatientsController#load as JS,这是正确的行为。

<%= link_to image_tag(sender.avatar.url(:thumb), class: "wht admintabletrtd1image-none click_load"), patient_load_path(sender.id), :remote => true, :alt => "user"%>

但是,我页面中的另一个 link_to 助手(见下文)却没有。单击时,它会输出Processing by PatientsController#load as HTML,并导致模板丢失错误。

<%= link_to :controller => n.specific.subject[:type].pluralize, :action => :load, :id => n.specific.subject[:id], :remote => true do %> // for the sake of this example, it becomes <%= :controller => 'patients', :action => :load, :id => 4, :remote => true do %>

它们都输出相同的链接,patients/4/load。我在这里缺少什么?

最佳答案

第二个链接的方法签名是错误的。

link_to(body, url, html_options = {})
# url is a String; you can use URL helpers like
# posts_path

link_to(body, url_options = {}, html_options = {})
# url_options, except :method, is passed to url_for

link_to(options = {}, html_options = {}) do
# name
end

link_to(url, html_options = {}) do
# name
end

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

在第一个链接中,您将文字字符串作为 url 传递。第二种使用更古老、更冗长、非面向资源的语法。

省略分隔 url 选项和 html 选项的方括号(remote: true 实际上只是一个数据属性)会给出错误的链接:

link_to "WRONG!", controller: "articles", id: "news", class: "article"
# => <a href="/articles/index/news?class=article">WRONG!</a>

第二个例子应该是这样的:

link_to({ 
controller: n.specific.subject[:type].pluralize,
action: :load,
id: n.specific.subject[:id]
}, { remote: true }) do # ...

但是您可能想让 Rails 多态路由助手为您完成找出 Controller 的工作:

link_to([n.specific, :load], remote: true) do # ...

关于javascript - 同一 link_to 助手的不同行为 - 模板丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35313937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com