0

first of all, im still learning Ruby on rails, currently im working on rails 4.2.6.

I have a problem with my code, i have a modal where i want to show the information dynamically with ajax (Calling a method in a controller and showing the information on the modal without closing it) but i have a problem, i cant call any function on the modal.

.col-xs-7
  %h5="#{period.name}"
.col-xs-5
  %form
    .input-group
      %input.form-control{type: "text", placeholder: "#{t('reports.periods')}"}/
      .input-group-btn
        %button.btn.transparent-button.dropdown-toggle{"data-toggle" => |
                                                         "dropdown", |
                                                       type: "button"} |
          = svg_icon('icon-arrow-down')
        %ul.dropdown-menu.pull-right
          - @batch_id.periods.each do |period|
            %li
              = link_to period.name, '#', :onclick => "myFunction", id: "#{period.id}_id", value: period.id     

.row.actions-container
.col-xs-12
  %h4="#{@student_id.name}"
  %hr
.row.actions-container
.col-xs-12
  %form
    = label_tag t('batches.observations')
    %br
    = text_area_tag 'rejection_comments', @comment.try(:comment) , class: 'form-control', :rows => 10, :cols => 30

- content_for(:page_javascript) do

  :coffeescript
  myFunction = ->
    alert 'hello world'
    return

Right now im trying only to call the function 'MyFunction' after i select a period but it throws an error:

myFunction is not defined

But if i do this:

  - @batch_id.periods.each do |period|
    %li
      = link_to period.name, '#', :onclick => "alert('hello')", id: "#{period.id}_id", value: period.id     

It works...

Can someone tell me what im doing wrong? I want to do the ajax thing as soon as possible.

Ps: The comment part on the text area is the info i want to get from the controller dynamically. Thanks!

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Execute summary: your `myFunction` is hidden inside a function wrapper [to prevent scope problems](http://coffeescript.org/#lexical-scope). Define your function as `window.myFunction = ...` or `@myFunction = ...` or better, use jQuery to bind the function instead of trying to use an old-school `onclick` attribute. – mu is too short Nov 24 '16 at 05:48
  • hey @muistooshort that didnt work for me :/ still says myfunction is not defined – Andres Fernandes Nov 24 '16 at 23:35
  • Which didn't work? And why not use jQuery to bind everything? What does the generated HTML look like? – mu is too short Nov 25 '16 at 00:17
  • neither window.myFunction nor @ myFunction, im trying something new, i will write you if cant advance with this, i'm pretty lost, sorry for the annoyment :P i'm still learning all of this world @myistooshort – Andres Fernandes Nov 25 '16 at 00:36

0 Answers0