【问题标题】:Rails - using url: versus href: when sending a path to the controller. What makes them different?Rails - 在向控制器发送路径时使用 url: 与 href: 。是什么让他们与众不同?
【发布时间】:2018-04-06 18:40:12
【问题描述】:

这是我之前的一个问题的后续问题: Rails 5 - use collection_radio_buttons to open a partial with nested models?

我们能够通过radio_button 发送请求以打开部分内容,该部分内容与被点击的按钮相关。 (耶!)AFAIK 的主要变化是我们在按钮的参数中将 href: 替换为 url:

我还不明白这两件事的执行方式有何不同。

这是每个版本的sn-ps:

<% 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 %>

特别是(rta 是一回事,只是在不同的循环中):

 href: system_applications_path(:system_id => rt.id, :schedule_id => params['id'])

对比:

 url: system_applications_path(:system_id => a.id, :schedule_id => params['id'])

【问题讨论】:

    标签: ruby-on-rails url checkbox href


    【解决方案1】:

    简而言之(这种见解来自一个相关问题,您可能会在右侧边栏中看到该问题)--

    url: 表示“这是用于值的数据; 而href: 的意思是“去这个位置”。

    因此,当您使用url: 单击按钮时,它会获取该值并以您使用的任何方法使用它。如果按钮改用href:,它会立即尝试转到href 指向的任何位置。

    这应该是 RoR 101 级别的东西,但我只是错过了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 2021-08-27
      • 2013-05-26
      相关资源
      最近更新 更多