Search code examples
jqueryruby-on-railscoffeescriptrefreshturbolinks

Rails JavaScript only loads when you Refresh Page, not on the original page load, what's wrong?


The code works when you refresh the page but not on original load.

Pinterest Clone I'm building. Rails App 4.1.4.

.

What is supposed to happen

When you click a Pin from the index page, it takes to SHOW page.
SHOW page has some JavaScript which resizes picture / DIV.

.

Problem:

Almost every time I click a Pin from the index page, the javascript does not run (does not resize the image / div).

BUT if I click REFRESH with the web browser the script seems to always run just fine (the images / Div get's resized just fine).

.

Link to js on github: https://github.com/growthcode/pinterest/blob/master/app/assets/javascripts/pins.js.coffee

Here it is on Heroku to try it out: https://growthcode-pinterest.herokuapp.com/

.

html of the SHOW pin:

<div class="container">
  <div class="row">
    <div id="showPinContainer" class="col-xs-12">
      <div id="showPin" class="well">
        <%= image_tag @pin.image, class: "show-image" %>
        <p class="description"><%= @pin.description %></p>
        <p><%= @pin.user.name %></p>
        <% if current_user == @pin.user %>
        <%= link_to 'Edit', edit_pin_path(@pin) %> |
        <% end %>
        <%= link_to 'Back', pins_path %>
      </div>
    </div>
  </div>
</div>

.

I wrote this in coffee script on rails, I'll list both coffee script and the 'Js2coffee' conversion of it.

Coffee Script:

# Pin Show page
$ ->
  # set jQuery object variables
  $windowObj = $(window)
  $showPinContainer = $("#showPinContainer")
  $showPin = $("#showPin")
  $showPinImg = $("#showPin img")
  $showPinHeight = $("#showPin").outerHeight()
  $showPinImgWidth = $showPinImg.outerWidth()

  setShowPinContainerHeight = ->
    if $showPinHeight > 450
      $showPinContainer.outerHeight( $showPinHeight )
    else
      $showPinContainer.outerHeight( 450 )

  setShowPinContainerWidth = ->
    if $showPinImgWidth < 200
      $showPinImg.outerWidth( 200 )
    else if $showPinImgWidth > 300 
      $showPinImg.outerWidth( 300 )

  setShowPinContainerSize = ->
    setShowPinContainerHeight()
    setShowPinContainerWidth()

  setShowPinContainerSize()

.

The Js2coffee.com conversion of the above coffee script:

$(function() {
  var $showPin, $showPinContainer, $showPinHeight, $showPinImg, $showPinImgWidth, $windowObj, setShowPinContainerHeight, setShowPinContainerSize, setShowPinContainerWidth;

  $windowObj = $(window);
  $showPinContainer = $("#showPinContainer");
  $showPin = $("#showPin");
  $showPinImg = $("#showPin img");
  $showPinHeight = $("#showPin").outerHeight();
  $showPinImgWidth = $showPinImg.outerWidth();

  setShowPinContainerHeight = function() {
    if ($showPinHeight > 450) {
      return $showPinContainer.outerHeight($showPinHeight);
    } else {
      return $showPinContainer.outerHeight(450);
    }
  };

  setShowPinContainerWidth = function() {
    if ($showPinImgWidth < 200) {
      return $showPinImg.outerWidth(200);
    } else if ($showPinImgWidth > 300) {
      return $showPinImg.outerWidth(300);
    }
  };

  setShowPinContainerSize = function() {
    setShowPinContainerHeight();
    return setShowPinContainerWidth();
  };

  return setShowPinContainerSize();
});

This is my first question on Stack Overflow, so if there is anything else I can provide that would help, please let me know. Thank you!


Solution

  • Try disabling Turbolinks. See the Rails Guides documentation on Turbolinks for more information.