1

My plan was to update a partial via a klick-event where I fetch some data via ajax. So i opened my assets/javascripts directory and put the code in the js file of the module I planed to call it on. I managed to fetch the data and to call some "html() or text()" on the div. But as soon as I tried to call

.html("<%= escape_javascript( render( :partial => 'job') ) %>")

my controller told me unknown method "render". So I red in many other Threats that the assets directory was not build for static content. But now comes the question where to put it if not in assets? I tried to put it in the view/model folder (with the same name as the aktion 'show'). But it wouldn't load. Yes I told the Controller in the show action to respond_to format.js.

So where should I put the js-File and how to call it? Or is there even a method to let it in the assets-directory and beeing able to call render?


Thanks for your answer! Sadly I do exactly that but I doesnt work. So here is my Code:

/app/views/events/show.js.erb.coffee:

$(document).ready ->
  url = window.location
  url = String(url).split('/')
  event_id = url[$.inArray( "events" , url) + 1]
  $('.position').click ->
    position_id = $(this).attr("id")
    $.ajax '/events/'+event_id+'/eventpositions/'+position_id+'/eventjobs'
      dataType: 'json'
      success: (result)->
        $( '#'+position_id ).find('.jobs').html("<%= escape_javascript( render( :partial => 'job') ) %>")
        alert result[1].job_id

/app/views/events/show.html.haml:

%p#notice{:xmlns => "http://www.w3.org/1999/html"}= notice
%p
  %b Name:
  = @event.name
  \ 
  %b Plz:
  = @event.plz
%p
  %b Personal:
  - @eventpositions.each do |p|
    .position{:id => "#{p.id}"}
    %b
      Position: #{Position.find(p.position_id).name} Anzahl: #{p.quantity} Aufträge: #{p.eventjobs.all.count}
    .jobs

  = link_to 'Neuer Auftrag', new_event_eventposition_eventjob_path(@event.id,p.id)
  %br
  %br
= link_to 'Neue Position', new_event_eventposition_path(@event.id)
= link_to 'Edit', edit_event_path(@event)
|
\#{link_to 'Back', events_path}

/app/views/events/_job.html.haml:

.jobs
  - eventjobs.each do |j|
    User: #{User.find(Job.find(j.job_id).user_id).email}
    %br

/app/controllers/events_controller.rb:

def show
  @agency = Agency.find(current_agent.agency_id)
  @event = @agency.events.find(params[:id])
  @eventpositions = @event.eventpositions.all

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @event }
    format.js
  end
end

So the Code does work without Javascript. All renders and is fine. But my main Problem now is that it wont even react to a click event if I place it in the app/views/events Folder. As soon as I place it in the assets directory I works like a charm, but wont render. So what am I missing? Why does my js Code does not get loaded?

Thanks a lot for your help!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jan
  • 47
  • 7

1 Answers1

1

Generally for this kind of thing, I'll name it the same as a particular action, such as show.js.erb, and then use the format.js as you have in the controller. If you're using coffeescript, it will need to be named show.js.coffee.erb. If this does not work, can you post the code of your view where this onclick event is setup?

Edit for using the show javascript code

Because you're using the show action, should just be able to do this:

= link_to "Show", @event, :remote => true, :format => :js

Adjust the event variable there as you need to, but it should work

agmcleod
  • 13,321
  • 13
  • 57
  • 96
  • Thanks a lot for your answer! Sadly I do exactly that and it doesn't work, most probably I miss something. – Jan Sep 12 '12 at 10:25
  • Still doesnt fix it for me. Even if I change it to a plan .js File (or a js.erb) it doesnt load or do anything with it. Even when I try to execute smth like: $('.position').click(function() { return alert ( 'Testing' ); });What could tell RoR not to load it? – Jan Sep 12 '12 at 16:03
  • Had a better look at your code, where are you trying to load this js file? You mentioned a click event, can you post the code that contains this click event? Thanks. – agmcleod Sep 12 '12 at 20:03
  • I try to load it with this one /app/views/events/show.js.erb.coffee (see Code below) but even if I Just make the click event as posted before nothing happens at all. So I guess the loading part is the Problem! Where do I tell Rails to include this js on this side? – Jan Sep 12 '12 at 22:56