0

I want to add a datepicker to my HAML markup. I started with that question and tried 2 approaches to add an script:

%h3
  Please enter the following information:
= form('/some_path', :post)
  = input(:date, :start_date, class: "formbox")
  = input(:date, :end_date, class: "formbox")
  = submit('Submit', class: "button")

%script(type="text/javascript")
  $(function() {$( "#start_date, #end_date" ).datepicker({format: 'yyyy-mm-dd'});});

and

:javascript
  $(function() {
    $( "#start_date, #end_date" ).datepicker({format: 'yyyy-mm-dd'});
  });

This one was finally correct, problem was in form part.

Both produce an error syntax error, unexpected keyword_ensure, expecting end-of-input on line :javascript or %script(type="text/javascript")

How to fix that and add the script?

1 Answer 1

1

%script tag expects curly brackets; in the latter example there is =content_for tag needed:

%script{data:{'foo'=> 'bar'}, 'type'=> "text/javascript"}
= content_for :javascripts do
  :javascript
    $(function() {
      $( "#start_date, #end_date" ).datepicker({format: 'yyyy-mm-dd'});
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Tried your answer - got the same syntax error on $(function() { line.
Thanks for attention, the problem was in form section, replaced with helper tags and got it working. Also, :javascript keyword worked just as is, no =content_for was needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.