简体   繁体   中英

Ruby on Rails - Remove/Replace some characters in string

I use nested_form along with bootstrap tab . To making bootstrap tab work properly I need to give each tab a unique id, otherwise only one of the 4 tabs will work correctly.

I have been able to get the id of each nested form input and give it as a unique id to each tab by using this:

- field_title = f.text_field :title
- id2 = field_title.match(/id="(?<id>[^"]*)"/)

In my bootstrap tab I have used this solution:

.nav-tabs-custom
  %ul.nav.nav-tabs
    %li.active
      %a{"data-toggle" => "tab", :href => "#tab_1_upl_#{id2}"} Upload
    %li
      %a{"data-toggle" => "tab", :href => "#tab_2_url_#{id2}"} link
    %li
      %a{"data-toggle" => "tab", :href => "#tab_3_lib_#{id2}"} image library

  .tab-content
    %div{id: "tab_1_upl_""#{id2}", class: " tab-pane active"}
      image

    %div{id: "tab_2_url_""#{id2}", class: " tab-pane"}
      link

    %div{id: "tab_3_lib_""#{id2}", class: " tab-pane"}
      library

But the issue now is that, the id for anchor is like:

id=" #tab_2_url_id="table_choices_attributes_1464338477560_title" "

As you can see this part: ="table_choices_attributes_1464338477560_title" is the issue here and prevent the tab to work properly.

How can I only get the text inside the id or just replace = " ?

I have tried with gsub , tr , tr! and getting error undefined method .

Thanks for help.

tr or gsub can not be used with a variable. So what I did was to use it outside of the string like code blow:

%div{id: "tab_1_upl_""#{id2}".tr('"=', ''), class: " tab-pane active"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM