I'm using these codes,
#In timetable/new.html.erb
<%= select_tag :department, options_for_select(
@departments.collect{ |d| [d.department_name, d.id]}, nil),{ id: "department_select" } %>
#In timetable controller
def update_lectures
if params[:department].to_i == 0
@lectures = Department.find_by(department_name: params[:department]).l ectures
else
@lectures = Department.find(params[:department]).lectures
end
respond_to do |format|
format.json { render :json => @lectures.to_json }
end
#In javascript
$("#department_select").change( function() {
$.ajax({
url: window.location.origin + '/timetable/update_lectures',
dataType: "json",
data: $("#department_select").serialize(),
success: function(data){
var str = '';
for(var i = 0; i < data.length; i++) {
str += '<li>' + data[name] + '</li>';
}
$('#lecture-container-body').html(str);
}
});
});
I want to know in #In javascript how can I read the 'data'?
When I use data[name] => "undefined"
When I use data[lecture_name] => "Uncaught ReferenceError: lecture_name is not defined" in console
@lectures will have these columns: id, lecture_name, lecture_division, passfail, .. etc.
(there is no 'name' column)
HELP please
datato the console and see what it looks like? the structure of what data is will be what will determine how you use it