6

The functionality I plan on doing is to insert some form elements depending on a number chosen from a select tag.

I have a select tag called for number_of_passengers, and i plan to dynamically append new passenger fields for the number chosen. Say I select 2 from number_of_passengers, then 2 forms should appear in a fieldset. these forms contain name, age weight etc.

I tried following this:

call a rails function from jquery?

and just converted it to haml-speak but I get errors whenever I use the :javascript tag. Also I don't think I can "escape" the javascript tag once I am in it

:javascript
  $('#number_of_passengers').change(function() {
    var $num_of_passengers = $(this).val();
    for($i=0; $i<$num_of_passengers;$i++) {
      $('.passenger-info ul').append('<%= escape_javascript( render :partial => "new_passenger", :locals => {:booking => @booking }) %>');
    }
  })

also since I am in a form_for, how do I pass the @booking variable to the local? It seems really complicated and I'm planning of doing the dirty way out of just looping 20 times(20 max passengers) then just hide/show them depending on the selected number. But that's too dirty don't you think?

Community
  • 1
  • 1
corroded
  • 21,406
  • 19
  • 83
  • 132

3 Answers3

12

To get the interpolation working you need to do something like

!= "$('.passenger-info ul').append('#{escape_javascript( render :partial => 'new_passenger', :locals => {:booking => @booking })}');

so simpler said: add the != to the start of the line, and include the string between double quotes.

Your question about the @booking: I suggest you investigate some nested forms examples to make it clearer for you (e.g. this and this railscast).

nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • hmm thanks! what does the != mean? I'll just bookmark this for now and check in the future because i just used a hardcoded js(inserted the form html manually...dirty but it works). it will be refactored soon though – corroded Aug 17 '10 at 11:29
  • The `!=` means unescape HTML, so it will insert the ruby string as is, without escaping it. As opposed to the normal `=` which evaluates and escapes the string, for safe inclusion in HTML. More here: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#unescaping_html – nathanvda Aug 17 '10 at 11:44
  • 1
    Even easier... use the :plain filter (not :javascript). – Karl Feb 17 '11 at 17:57
  • Is the leading " necessary? – Ziggy Apr 04 '13 at 21:47
3

i actually managed to do this by adding:

respond_to do |format|
  format.js { render :partial => "new_passenger" }
end

so even if the request was in js, it will return an html partial. Nifty

corroded
  • 21,406
  • 19
  • 83
  • 132
  • Did you ever try my answer? In case it was not clear: you were using erb-style interpolation inside haml. But glad you found something that worked. – nathanvda Feb 18 '11 at 07:37
  • I'm trying to make sense of what you did here, also interested in rendering some HTML based on what's inside a javascript conditional. Where does "new_passenger" exist? And your answer; where did you place this code? – Tass Nov 14 '11 at 22:33
  • new_passenger exists in the same view folder as the controller, so in my case, bookingscontroller called this so it was in app/views/bookings – corroded Nov 15 '11 at 10:30
0

Rails syntax changes all the time but shouldn't this

render :partial => "new_passenger", :locals {:booking => @booking })

be

render :partial => "new_passenger", :locals => {:booking => @booking })

You can escape javascript within the javascript block because you're evaluating ruby tags.

Which error messages are you receiving?

The latter part of your question isn't clear to me but is this what you're looking for:

- form_for @user do |f|
  # f.object is equal to @user
mark
  • 10,316
  • 6
  • 37
  • 58
  • Hi thanks for your reply. I got quit caught by a sickness. anyway, I fixed the typo already but I'm still stumped. I'll be editing the question in a bit and show you the problem – corroded Aug 09 '10 at 14:58