I'm a Ruby on Rails beginner, following through the Pinterest clone guide by John Elder and I'm stuck on trying to apply masonry-rails and jquery.
I'm operating on c9.io and I have installed the required gems and used bundle install.
The problem I am having is that the display of my pins are still vertical and not responsive even though there are no errors displayed in loading.
What am I doing wrong?
Here is my code:
application.css
*= require 'masonry/transitions'
*= require_tree .
*= require_self
*/
pins.js.coffee
$ ->
$('#pins') .imagesLoaded ->
$('#pins') .masonry
itemSelector: '.box'
isFitWidth: true
pins.css.scss
#pins {
margin: 0 auto;
}
.box {
margin: 5px;
width: 214px;
}
.box img {
width: 100%;
}
index.html.erb
<div id="pins" class="transitions-enabled masonry">
<% #pins.each do |pin| %>
<div class="box">
<div class= "panel panel-default">
<%= link_to image_tag pin.image.url (:medium) %><br/>
<div class="panel-body">
<%= pin.description %><br/>
</div>
<% if pin.user == current_user %>
<dsv class="panel-footer">
<%= link_to 'Edit', edit_pin_path(pin) %><br/>
<%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
application.js
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require bootstrap
//= require masonry/jquery.masonry
//= require_tree .
I finally managed to find a solution to the problem!
I just tried adding the additional lines to the application.css file:
*= require 'masonry/basic'
Adding this in is said to be optional in the textbook but they are in fact very necessary to ensure the displays work properly (in my opinion...I'm a beginner).
Related
Ive been working on adding daterangepicker (https://www.daterangepicker.com/#usage) to a rails app, and successfully got it working. However today, out of nowhere, Ive started getting the Uncaught TypeError: $(...).daterangepicker is not a function error in my console, and the page breaks. I havent changed my application.js, gemfile, nor stylesheets since it was working, hoping to get some assistance here. From doing some research it seems that the function is being loaded before jquery is, but I am mystified as to why this is happening.
Gem file:
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'bootstrap', '~> 4.3.1'
gem 'momentjs-rails'
gem 'bootstrap-daterangepicker-rails'
application.js
//= require rails-ujs
//= require jquery
//= require jquery-ui
//= require moment
//= require bootstrap
//= require daterangepicker
//= require_tree .
application.scss
*= require jquery-ui
*= require daterangepicker
daterangepicker.js
$(function() {
$('.daterange').daterangepicker({
opens: 'left',
locale: { format: 'YYYY/MM/DD'}
}, function(start, end, label) {
console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
});
});
config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( daterangepicker.js )
View:
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<%= javascript_include_tag "daterangepicker.js" %>
<%= form_with scope: :export, url: history_export_path, remote: true do |f| %>
<%= f.text_field :created_at_range, :class => 'form-control daterange', value: #created_at_range %>
<%= f.submit "Filter Histories"%>
<% #export.date_scope(#start_date, #end_date).order(sort_column + " " + sort_direction).each do |history| %>
<tr>
<td><%= history.success %></td>
<td><%= history.message.to_s %></td>
<td><%= history.created_at %></td>
</tr>
<% end %>
<% end %>
First think your daterangepicker js file has confusion with the daterangepicker plugin file. rename it or add a script tag at the end of your view file.
add javascript_include_tag and stylesheet_link_tag to application.html.erb
#app/views/layouts/application.html.erb
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
delete script tags, link tag and javascript_include_tag 'daterangepicker' your view, they are not necessary if you add dependency in your application.js and application.css
I'm not sure if this is a bug or what, but the notice messages are not disappearing and I have looked at a bunch of other examples, none of them explain my case:
like these:
Why flash message won't disappear?
rails 4 -- flash notice
Rails flash notice won't go away in Safari?
devise gem flash messages
I used the Rails_Composer and I promised myself not to use it again, because it just caused me a nightmare
This is what I have loaded in my files:
application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require wiselinks
//= require_tree .
application.css.scss
/*
*= require_tree .
*= require_self
*/
framework_and_overrides.css.scss
#import "bootstrap.min";
and as the Rails_Composer generates the files, this is what we have in the layout:
_messages.html.eb
<%# Rails flash messages styled for Bootstrap 3.0 %>
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>
any idea how to fix this mess ?
I have this code...
<%= simple_form_for my_object, url: object_path(object_id), html: {class: "form account-form"}, remote: true do |f| %>
<div class="row">
<div class="col-md-4">
<%= f.input :numero_matricula, label: "Núm. de Matricula" %>
</div>
<div class="col-md-4">
<%= f.input :rg, label: "Rg", required: true %>
</div>
<%= f.button :submit, class: 'btn-default', disabled: false %>
<% end %>
When i click at the submit button, the request is html, but i need to call the update.js.erb. In my applicatin.js i have this:
//= require jquery
//= require jquery_ujs
//= require icheck
//= require chosen
//= require jquery.mask
//= require jquery.maskMoney
//= require bootstrap
//= require smart_listing
//= require bootstrapValidator
//= require bootstrap-datetimepicker
//= require locales/bootstrap-datetimepicker.pt-BR
//= require editable/bootstrap-editable
//= require editable/rails
//= require_tree .
This is the controller:
class FuncionariosController < ApplicationController
respond_to :html, :json, :js
def update
#Object = Funcionario.find(params[:id])
#object.update_attributes(params[:object])
end
end
does anyone know what I'm doing wrong?
If you need more information I am available...
I have some code for attending/withdrawing from a competition:
<% if #competition.users.exclude?(#user) %>
<%= link_to 'Attend Competition', attend_competition_path(#competition.id), :method => :post %>
<% else %>
<%= link_to 'Withdraw', withdraw_competition_path(#competition.id), :method => :post %>
<% end %>
When I click on the action I go to an error page:
No route matches [GET] "/competitions/1/withdraw"
Why isn't it doing a POST request? How do I fix this?
Not sure if it effects it, but my current js is
//= require jquery
//= require bootstrap
//= require turbolinks
//= require_tree .
thanks
I'm using the assets pipeline from Rails 3.1 and I want to include some javascript files only if it's the development environment.
Example:
//= require application/jquery
//= require application/jquery_ujs
// next ones only for development environment
//= require application/badglobals
//= require application/consul
Its there a standar way of doing this? Any suggestions?
Update
Looking at the Sprockets current documentation, seems like there is not a way to do this.
Why not just require these in the view? Is it important that they are loaded in the asset? To load them in the view:
<% if Rails.env.development? %>
<%= javascript_include_tag "application/badglobals" %>
<%= javascript_include_tag "application/consul" %>
<% end %>
If you rename your application.js file (or whichever file you're calling //= require ... in) to application.js.erb, you can take advantage of require_asset. i.e:
//= require application/jquery
//= require application/jquery_ujs
<% if Rails.env.development? %>
<%= require_asset 'application/badglobals' %>
<%= require_asset 'application/consul' %>
<% end %>
Source: https://github.com/sstephenson/sprockets/issues/90