1

I'm still working on learning Rails, and I have a page with team information that will get updated based on a team's icon click, which fires an ajax call to the controller to populate some tabs.

I've read some good info about how to use format.js in the controller to render a partial from a js.coffee or js.erb file.

The problem I'm running into is in the coffeescript I think. Right now, I'm getting some data called @schedules from the controller, and passing it to a schedule.js.coffee file that should populate a partial for each record returned and attach it to a table.

// schedule.js.coffee
$.each @schedules, (schedule) ->
    ($ '#schedule_data').append("<%= j render(partial: 'schedules/schedule', locals: { s: schedule }) %>")

This throws an error

`> undefined local variable or method `schedule' for #<#<Class:0x007fe535cd2900>:0x007fe535d32a30>`

I tried simplifying the coffeescript to just log the output:

$.each @schedules, (schedule) ->
    console.log(schedule)

but this prints nothing.

Am I missing something? I am very inexperienced with coffeescript, but it seems like I should be getting some data-- I verified that the schedule items do exist for this team item.

Community
  • 1
  • 1
user101289
  • 9,888
  • 15
  • 81
  • 148
  • 1
    Please post your related controller code and the partial file. – Pavan Jun 11 '14 at 04:29
  • I don't think you are meant to use rails variables in coffeescript, so $.each @schedules, appears wrong to me... – Ruby Racer Jun 11 '14 at 04:49
  • try a simple console.log or an alert statement without any loop inside your coffee file – Mandeep Jun 11 '14 at 06:06
  • 1
    I think @RubyRacer is right, you're mixing your coffeescript with your ruby where you're calling `$.each`... What if you do `$.each <%= j @schedules %>, (schedule) -> ` etc? I doubt it would do what you want, but you should get something... – Helios de Guerra Jun 11 '14 at 06:20

1 Answers1

0

ERB

Further to RubyRacer's comment, the most important thing to note here is the use of Rails code inside your JS. Although processing .js.coffee and .js.erb files in the backend of your system does allow for Rails code to be used, it has to follow the same syntax as it would in an html.erb (same preprocessor):

 // schedule.js.coffee
<% @schedules.each do |schedule| %>
    $('#schedule_data').append("<%= j render(partial: 'schedules/schedule', locals: { s: schedule }) %>")
<% end %>

Error

A more curious problem is your error:

undefined local variable or method `offer' for #<Class:0x007fe535cd2900>:0x007fe535d32a30>

This suggests you have a problem inside your model (where you're referencing the offer method of your schedule object). I think the problem would be caused by your incomplete JS previously (as you weren't passing the right data)

If the problem persists after attempting my fix above, please comment & I'll help!

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    @RichPeck-- sorry, you're correct in noting the error message. I posted late at night after staring at the problem far too long and used the wrong variable name when I printed the error. It is fixed above. – user101289 Jun 11 '14 at 15:22
  • Thanks so much for your help Rich-- that was indeed the problem. I was mixing js and ruby. However, the script is not throwing an error but is not doing anything either. Does coffeescript require a `document.ready` statement or something of that kind to trigger it? Or should this be called automatically by the controller? – user101289 Jun 11 '14 at 15:38
  • Here is the returned / compiled coffeescript, which seems correct but does nothing: `(function() { jQuery(function() { alert('yo'); return $('#schedule_body').append("\n July 1<\/td>\n Oklahoma State Cowboys<\/td>\n OK City<\/td>\n <\/tr>"); }); }).call(this);` – user101289 Jun 11 '14 at 15:49