【问题标题】:gird view not working in rails网格视图在 Rails 中不起作用
【发布时间】:2017-12-20 13:01:33
【问题描述】:

我对 Rails 很陌生,我正在通过创建一个网络应用程序自学,现在我正在构建一个类似 pintrest 的网络应用程序。

一切都运行良好,但我希望引脚处于网格视图中。为此,我尝试使用masonry-rails Gem。 但这似乎不起作用。

我的索引文件

<link href="/assets/lposts.css.scss" rel="stylesheet">
<link href="/assets/pins.js.coffee" rel="javascript">
<div class="transitions-enabled" id="pins">
  <% @posts.each do |post| %>
    <div class="box panel panel-default">
      <%= link_to (image_tag post.picture.url), post %>
      <h2>
        <%= link_to post.title, post %>
      </h2>
      <p class="user">
        Submitted by
        UserName
      </p>
    </div>
  <% end %>
</div>

我的 css 文件

body {
    background: #E9E9E9;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 100;
}

nav {
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.22);
    .navbar-brand {
        a {
            color: #BD1E23;
            font-weight: bold;
            &:hover {
                text-decoration: none;
            }
        }
    }
}

#pins {
  margin: 0 auto;
  width: 100%;
  .box {
      margin: 10px;
      width: 350px;
      box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.22);
      border-radius: 7px;
      text-align: center;
      img {
        max-width: 100%;
        height: auto;
      }
      h2 {
        font-size: 22px;
        margin: 0;
        padding: 25px 10px;
        a {
                color: #474747;
        }
      }
      .user {
        font-size: 12px;
        border-top: 1px solid #EAEAEA;
            padding: 15px;
            margin: 0;
      }
    }
}

#edit_page {
    .current_image {
        img {
            display: block;
            margin: 20px 0;
        }
    }
}

#pin_show {
    .panel-heading {
        padding: 0;
    }
    .pin_image {
        img {
            max-width: 100%;
            width: 100%;
            display: block;
            margin: 0 auto;
        }
    }
    .panel-body {
        padding: 35px;
        h1 {
            margin: 0 0 10px 0;
        }
        .description {
            color: #868686;
            line-height: 1.75;
            margin: 0;
        }
    }
    .panel-footer {
        padding: 20px 35px;
        p {
            margin: 0;
        }
        .user {
            padding-top: 8px;
        }
    }
}

textarea {
    min-height: 250px;
}

我的 js 文件

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

$ ->
  $('#pins').imagesLoaded ->
    $('#pins').masonry
      itemSelector: '.box'
      isFitWidth: true

在我的 application.scss 中

*= require 'masonry/transitions'

在我的 application.js 中

//= require masonry/jquery.masonry

【问题讨论】:

  • 第一次学习 Rails 基础的最佳方法请遵循 assetslayout 教程。

标签: ruby-on-rails ruby-on-rails-3 masonry


【解决方案1】:

您包含 js 资产的方式是错误的。

请使用 js 文件包含

<%= javascript_include_tag 'pins' %>

并将pins.js.coffee 文件放在预编译资产路径中

Rails.application.config.assets.precompile += %w(articles.js)

您可以将资产放在 application.js 中

//= require pins

希望这对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多