I had a plan to update a partial using a click event where data was fetched via ajax. I navigated to my assets/javascripts directory and placed the code in the js file of the module I intended to call it on. I successfully fetched the data and performed some "html() or text()" operations on the div. However, when I tried to execute
.html("<%= escape_javascript( render( :partial => 'job') ) %>")
my controller returned an error stating that the method "render" was unknown. After reading several other discussions, I learned that the assets directory was not meant for static content. This led me to the question: where should I place it if not in assets? I attempted to put it in the view/model folder (with the same name as the action 'show'), but it failed to load. Yes, I informed the Controller in the show action to respond_to format.js.
So, where should I locate the js File and how should I invoke it? Or is there a way to keep it in the assets directory and still be able to call render?
Thank you for your response! Unfortunately, despite following your instructions, it still doesn't work. Here's 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
The code works without JavaScript. Everything renders correctly. However, my main issue now is that it doesn't respond to a click event when placed in the app/views/events Folder. If I move it to the assets directory, it works perfectly fine but doesn't render. What am I missing here? Why isn't my js Code being loaded?
Appreciate your assistance!