【问题标题】:Convert html "href=" into "link_to" for Rails 4将 html "href=" 转换为 Rails 4 的 "link_to"
【发布时间】:2023-04-04 19:42:02
【问题描述】:

这是我要转换成link_to的代码

<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?category=Video+and+animation">Show me everything</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Intro">Intro</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Animation+%26+3D">Animation & 3D</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Editing+and+Post+Production">Editing and Post Production</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Other">Other</a></li>
</ul>

这就是它的样子

例如,您将如何转换

<li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Intro">Intro</a></li>

转入&lt;%= link_to "Intro", some_path(that goes to the above Url), method: :get %&gt;

或者你如何将这个方法::get添加到上面的html href中,我需要它来无限滚动,turbo链接会导致问题,所以我有使用方法::get

这是我用来创建包含子类别的类别的种子.rb

@category = Category.create!(name:  "Video and animation")

["Intro", "Animation & 3D", "Editing and Post Production", "Other"].each do |name|

@subcategory = Subcategory.create!(name: name, category_id: @category.id)

end

【问题讨论】:

  • 我必须保持一切原样,但是添加这个元素'method::get',我想我只有将上面的html转换成ruby link_to才能应用它,我可能错了当然。看到我在那里使用href=""所以我必须将它转换为link_to,否则以某种方式将"method::get"直接用于这个html href。
  • 不需要在link_to中给出(方法: :get )
  • 您可以在锚标签中添加data-method="get"。这将解决您的问题
  • 您可以通过在链接中添加 'data-no-turbolink' => true 来禁用这些链接的 turbolinks

标签: html ruby-on-rails ruby ruby-on-rails-4 model-view-controller


【解决方案1】:
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?category=Video+and+animation" data-method="get">Show me everything</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Intro" data-method="get">Intro</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Animation+%26+3D" data-method="get">Animation & 3D</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Editing+and+Post+Production" data-method="get">Editing and Post Production</a></li>
  <li role="presentation"><a role="menuitem" tabindex="-1" href="http://www.example.com/gigs?subcategory=Other" data-method="get">Other</a></li>
</ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多