1

I've been following this guide about multiple jquery time span sliders. Am trying to get the daily start value from my rails app. Am struggling to get the variables to insert into the jquery function correctly.

I have a field for each day of the week which includes the start time (per day). Eg:

 @location.active_monday
 @location.active_tuesday
 @location.active_wednesday

The jquery function to display the sliders looks like this:

_sliderhtml: function(day){
            var displayday = day.charAt(0).toUpperCase() + day.substr(1);
            var html = ""+ 
                '<div class="fieldrow spacer">' +
                    '<div>' +
                      '<input type="text" name="location['+day+'_on_wifi]" id="'+day+'-time1" value="xxxxxxx" > - '+

In the input value, I'm trying to insert the value from my model but I can't figure out the correct syntax. Have tried:

 value="<%= "@location.active_wednesday" %>

Which works, but I need the day replaced with '+day+'. For example:

 value="<%= "@location.active_'+day+'" %>

Is there a way to do this or do I need to rethink?

2 Answers 2

5

You need to rethink. You can't use your javascript variables (such as day, on your example) on ruby code, since it has already run. The values need to be on the rendered view if you need your javascript to be able to access it.

you can try something like this:

var active_values = {
  monday:    "<%= @location.active_monday %>",
  tuesday:   "<%= @location.active_tuesday %>",
  wednesday: "<%= @location.active_wednesday %>"
};

and then, on your JS function, just access the data:

'<input value="' + active_values[day] + '" />' ;
Sign up to request clarification or add additional context in comments.

3 Comments

You are a genius! Thanks. Just one tiny edit to your answer - inserted the ' ' around values. Thanks again.
Oh, you're welcome :) I didn't spot the quoting issue, can you point out on which line it is?
Tried to edit your post but was too short. Needed to place ' between the " and the +. Works a treat now!
1

what you will need to do is assign a javascript location variable.

var location = <%= @location.to_json %>;

Then in your javascript method you can use.

value=location["active_" + day];

5 Comments

probably better would be to add a method on your model. get_active_for_day(day) then embed that send call in that method
Actually I think it doesn't, since day comes as an argument on the javascript function, but maybe I misunderstood.
Glad I'm not the only one struggling!?!? Tried that, doesn't work. Also tried "<%= @location %>.'+day+'" Which just gives me #<Location:0x007ffae1dab478>.monday grrrrr
Still doesn't work actually. I missed that. You'll have to do something different. Can't mix ERB during the javascript runtime.
Yup, getting an undefined method 'day'. Tried a few incarnations. Thanks though.

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.