haml + jquery add a partial

855 views
Skip to first unread message

Eumir

unread,
Aug 5, 2010, 11:38:02 AM8/5/10
to Haml
Hi guys,

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:

http://stackoverflow.com/questions/261752/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?

Jeroen van Dijk

unread,
Aug 7, 2010, 4:49:34 AM8/7/10
to ha...@googlegroups.com
Hope you found an solution already, but what I see it that you are including erb tags in the javascript code. Change <%= %> to #{ } and I guess you should be able to get it working:

: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 }) }');
    }
 })

HTH 





--
You received this message because you are subscribed to the Google Groups "Haml" group.
To post to this group, send email to ha...@googlegroups.com.
To unsubscribe from this group, send email to haml+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haml?hl=en.


Eumir

unread,
Aug 9, 2010, 11:00:31 AM8/9/10
to Haml
Hi Jeroen,

It works now thanks! However, I don't know if it's haml or just my
server crashing when I add a @booking.passengers.build to the partial.
There's even no error message of sort, I just have to kill -9 when I
refresh the page

On Aug 7, 4:49 pm, Jeroen van Dijk <jeroentjevand...@gmail.com> wrote:
> Hope you found an solution already, but what I see it that you are including
> erb tags in the javascript code. Change <%= %> to #{ } and I guess you
> should be able to get it working:
>
> :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 }) }');
>     }
>  })
>
> HTH
>
>
>
> On Thu, Aug 5, 2010 at 5:38 PM, Eumir <imacaterpil...@gmail.com> wrote:
> > Hi guys,
>
> > 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:
>
> >http://stackoverflow.com/questions/261752/call-a-rails-function-from-...
>
> > 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?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Haml" group.
> > To post to this group, send email to ha...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > haml+uns...@googlegroups.com <haml%2Bunsu...@googlegroups.com>.

Eumir

unread,
Aug 9, 2010, 1:22:12 PM8/9/10
to Haml
Okay just a little wee bit of curiosity. I was doing

code like this for the partial:


- fields_for "booking[booking_passenger][]", my_passenger do |
passenger|
%li
%fieldset.passenger-info
%ul
%li
= passenger.label :first_name
= passenger.text_field :first_name

and I keep seeing the javascript call:

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

as this when i view source:

$('.passenger-info ul').append('');

so i scratch my head and try out a gazillion things and finally try to
do something stupid:

= fields_for "booking[booking_passenger][]", my_passenger do |
passenger|

and it suddenly worked. WHY? I mean we don't print out fields_for
right? its always written as <% fieldes_for %> in erb? so why the diff
in HAML?

Jeroen van Dijk

unread,
Aug 9, 2010, 3:04:29 PM8/9/10
to ha...@googlegroups.com
On Mon, Aug 9, 2010 at 7:22 PM, Eumir <imacate...@gmail.com> wrote:
Okay just a little wee bit of curiosity. I was doing

code like this for the partial:


- fields_for "booking[booking_passenger][]", my_passenger do |
passenger|
 %li
   %fieldset.passenger-info
     %ul
       %li
         = passenger.label :first_name
         = passenger.text_field :first_name

and I keep seeing the javascript call:

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

as this when i view source:

$('.passenger-info ul').append('');

so i scratch my head and try out a gazillion things and finally try to
do something stupid:

= fields_for "booking[booking_passenger][]", my_passenger do |
passenger|

and it suddenly worked. WHY? I mean we don't print out fields_for
right? its always written as <% fieldes_for %> in erb? so why the diff
in HAML?

This is not a change in Haml, this is a change in Rails. Since Rails3 block helpers that add content to the block that is yielded need <%= in erb and = in Haml. Maybe this is a good source for you to watch: http://railscasts.com/episodes/208-erb-blocks-in-rails-3 

 
To unsubscribe from this group, send email to haml+uns...@googlegroups.com.

Eumir

unread,
Aug 10, 2010, 2:24:41 AM8/10/10
to Haml
ohhhh so that's why. Thanks! you've been a great help!

On Aug 10, 3:04 am, Jeroen van Dijk <jeroentjevand...@gmail.com>
wrote:
> > haml%2Bunsu...@googlegroups.com<haml%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages