0

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

3
  • Can you add the code which calls findTimes? Commented Feb 13, 2012 at 12:05
  • Where do you see that error? After ajax submitted or before? Commented Feb 13, 2012 at 12:05
  • It happened after the ajax was submitted. Turns out I needed to declare a params key for 'matched_time' so that it would be a value for my controller. Commented Feb 13, 2012 at 13:20

1 Answer 1

2

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},
  ..});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.