ruby on rails - Generic way of using content_tag for multiple elements of the same kind -


i want generate html like,

<label for='field'>   label text   <span class='span1'> text 1 </span>   <span class='span2'> text 2 </span>   ... </label> 

i want call helper such as,

label_for 'field', :label => 'label text', :type1 => 'some text 1', :type2 => 'some text 2'

for tried like,

 content_tag(:label, opts[:label], :for => field_name)    ['span1', 'span2'].map { |i|       content_tag(:span, opts[i], :class => i) if opts[i] }.compact.joins('+').html_safe    }   end 

but not work (of course).

['span1', 'span2'] array fixed , user has option of choosing display many spans needed.

how can solve problem?

why not this?

def special_label_for(field_name, label_text, span_array)   content_tag "label", :for => field_name     concat label_text     span_array.each_with_index |span_content, index|       concat content_tag("span", span_content, :class => "span" + index.to_s)     end   end end  special_label_for :user_name, "user name", ["this first span", "and second span", "can have third span", "or many spans like"] 

haven't tested code, may need add/remove concat or html_safe rendering in view.


Comments

  1. william hill【VIP】guide to casino【WG】
    casino【WG98.vip】⚡,guide to casino,best online casino 카지노 bonus,best mobile casino,best william hill online casino betway login free spins no deposit free spins,freespins free

    ReplyDelete

Post a Comment

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -