I’m trying to have a user-created variable processed on the server and then sent back to the view and appended to a div.
my view using erb:
<div id = "times_div"></div>
<script>
function findTimes(current_time){
matched_time = current_time;
$.ajax({
type: "POST",
url: "match_times",
cache: false,
data: matched_time,
success: function(<%= @new_times %>){
$("#times_div").append(<%= @new_times %>);
}
});
};
</script>
my controller:
def match_times
reduce = Schedule.where(:date => matched_time)
hour_on_time = reduce.collect {|x| x.hour}
@new_times = hour_on_time
end
and my route:
match '/match_times', :to => 'appointments#match_times'
but I’m getting an ‘undefined local variable or method `matched_time’. Why isn’t the controller reading the ‘data’ property sent via Ajax?
Thanks
If you use jQuery ajax you should pass something like
then you can evaluate the parameter in the controller using
params[:matched_time]. You can also use Javascript objects instead of strings