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
$ajax({..,
data: "matched_time="+matched_time,
..});
then you can evaluate the parameter in the controller using params[:matched_time]
. You can also use Javascript objects instead of strings
$ajax({..,
data: {matched_time : matched_time},
..});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.