在制作元素的id时,如何强制content_tag_for使用破折号而不是下划线?

时间:2022-07-29 21:15:07

I'm using jQuery-UI's tabs, which don't seem to like the content_tag_for method of joining the record with the id using an underscore. Is there a way to force content_tag_for to use dashes instead of underscores without redefining content_tag_for in my application helper with some nasty gsub? Here's my code as it is.

我正在使用jQuery-UI的选项卡,它们似乎不喜欢使用下划线将id加入记录的content_tag_for方法。有没有办法强制content_tag_for使用破折号而不是下划线而无需在我的应用程序助手中使用一些令人讨厌的gsub重新定义content_tag_for?这是我的代码。

  <ul class="ui-tabs-nav">
    <% @preview_articles.each do |article| %>
      <%= content_tag_for(:li, article, 'nav-fragment', :class => "ui-tabs-nav-item #{'ui-tabs-selected' if article == @preview_articles.first}") do %>
        <%= link_to image_tag(article.photos.first.image.url(:thumbnail)), "#fragment-#{article.id}" %>
      <% end %>
    <% end %>
  </ul>

I need that li tag to have an id of nav-fragment-article-id whereas content_tag_for is creating an id like nav-fragment-article_id.

我需要li标签有一个nav-fragment-article-id的id,而content_tag_for正在创建一个像nav-fragment-article_id这样的id。

Any suggestions on getting jquery-ui's tabs to work with underscores are welcome, too.

有关获取jquery-ui选项卡以使用下划线的任何建议也是受欢迎的。

1 个解决方案

#1


3  

You can monkey patch ActionController::RecordIdentifier, like so:

你可以修补ActionController :: RecordIdentifier,就像这样:

module ActionController
  module RecordIdentifier
    JOIN = '-'.freeze
  end
end

Just stick this into an initializer somewhere and you're good to go, even though it'll generate a warning because the constant was defined before.

只需将其粘贴到某个地方的初始化程序中即可,因为该常量之前已定义,因此它会产生警告。

#1


3  

You can monkey patch ActionController::RecordIdentifier, like so:

你可以修补ActionController :: RecordIdentifier,就像这样:

module ActionController
  module RecordIdentifier
    JOIN = '-'.freeze
  end
end

Just stick this into an initializer somewhere and you're good to go, even though it'll generate a warning because the constant was defined before.

只需将其粘贴到某个地方的初始化程序中即可,因为该常量之前已定义,因此它会产生警告。