提问人:Leland 提问时间:10/25/2017 最后编辑:Leland 更新时间:1/31/2018 访问量:49
Rails - 在向控制器发送路径时使用 url: 与 href: 相对。是什么让它们与众不同?
Rails - using url: versus href: when sending a path to the controller. What makes them different?
问:
这是我之前一个问题的后续问题:Rails 5 - 使用 collection_radio_buttons 打开带有嵌套模型的部分?
我们能够发出发送请求以打开与正在单击的按钮相关的内容的部分。(耶!AFAIK 的主要变化是当我们在按钮的参数中替换为时。radio_button
href:
url:
我还不明白的是这两件事在执行上有何不同。
以下是每个版本的代码片段:
<% System.all.each do |rt| %>
<label>
<%= f.radio_button :system_id, rt.id, data:{:remote => true, 'data-target' =>
'@applicationList'}, href: system_applications_path(:system_id => rt.id,
:schedule_id => params['id']), class: 'remote-input', onclick:
"#applicationsList" %>
</label>
<% end %>
对:
<% @systems.each do |a|
<label class="btn btn-sm btn-default">
<%= f.radio_button :system_id, a.id, :data => {url:system_applications_path(:system_id
=> a.id, :schedule_id => params['id']), 'data-target' => '#applicationList'+a.id.to_s,
:remote => true} %>
<%= a.system_nm %>
</label>
<% end %>
特别是(和实际上是一回事,只是在不同的循环中):rt
a
href: system_applications_path(:system_id => rt.id, :schedule_id => params['id'])
与:
url: system_applications_path(:system_id => a.id, :schedule_id => params['id'])
答:
0赞
Leland
1/31/2018
#1
简而言之(这个见解来自一个相关的问题,你可能可以在右边的侧边栏中看到)——
url:
表示“这是用于该值的数据;
并表示“转到此位置”。href:
因此,当您单击按钮 using 时,它会获取该值并在您使用的任何方法中使用它。如果按钮改用,它会立即尝试转到指向的任何位置。url:
href:
href
这应该是 RoR 101 级别的东西,但我只是错过了它。
评论