0

Inside of a view in my rails app. I am trying to detect the screen size using JavaScript and render some rails content depending on the screen size.

Here is what I have:

<script>
if( $(window).width() < 1000){
'<%= @resize = true %>';
} else {
'<%= @resize = false %>';
}
</script> 

I keep getting false returned for the @resize variable even if the browser window size is less than 1000. How would I go about getting this to work?

1
  • What are you trying to show in the view exactly? The rails code is ran before javascript, so @resize is ultimately false before the if statement is even considered Commented Jan 5, 2015 at 22:39

1 Answer 1

1

You need to change things up:

  1. Write JS that sets a cookie containing the value you want based on the conditions.
  2. Read the value of that cookie in Rails.

Note the problem with this approach is that on the very first page load Rails won't have access to that cookie data. Only on the next request will it get the value. But there's no way around that.

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.