javascript - rails link_to_function help understanding -


i'm trying maintain quite old rails code (not written me), , struggling badly non-functional javascript can't working.

it uses link_to_function call see has been deprecated, , wondering people use replacement.

this code i'm trying fix:

link_to_function text, :class => "button new add_track" |page|   track = render(:partial => 'track', :locals => { :f => form, :track => track.new })   page << ""+     "var new_track_id = \"new_\" + new date().gettime();"+     "$('.track_list').append(\"#{ escape_javascript track }\".replace(/new_\\d+/g, new_track_id) );"+     "tentracks.admin.ui.set_track_sort_order();"+     "tentracks.admin.ui.setup_track_image_managers();" end 

i understand what's meant happen user clicks link, appends partial 'track' form onto page, don't see how meant work function 'link_to_function'.

from api docs, see message signature of:

  • 3.1.0 = link_to_function(name, function, html_options={})
  • 2.2.1 = link_to_function(name, *args, &block)

now i'm not 100% sure code written against, neither make sense me, in code i've got, can see 2 arguments:

  • arg1 = 'text',
  • arg2 = ':class => "button....

i see no other commas indicate arg 3.

there must i'm not understanding rails though because code did used work fine!

what people recommend how add form dynamically page in rails using js?

if explain what's happening in code, v useful, think there's fundamental part of rails dev that's stopping me understanding how simple things.

using details gave, i'm betting code written rails 2.2.x since link_to_function still accepts block. remember having override helper in rails 3 accept block.

there 2 solutions this. first 1 place js in 1 long line.

track = render(:partial => 'track', :locals => { :f => form, :track => track.new }) js = "var new_track_id = \"new_\" + new date().gettime();$('.track_list').append(\"#{ escape_javascript track }\".replace(/new_\\d+/g, new_track_id) );tentracks.admin.ui.set_track_sort_order();tentracks.admin.ui.setup_track_image_managers();"  link_to_function text, js, :class => 'button new add_track' 

that ugly solution.

with rails 3, obstrusive javascripts 1 have been discouraged. how should write it.

link_to text, '', :class => 'button new add_track'  <script type='text/javascript'>   $('.add_track').click(function() {     var new_track_id = "new_" + new date().gettime()      $('.track_list').append("#{escape_javascript render('track', :f => form, :track => track.new)}".replace(/new_\\d+/g, new_track_id))      tentracks.admin.ui.set_track_sort_order()     tentracks.admin.ui.setup_track_image_managers()   }) </script> 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -