I cannot get CoffeeScript working with Rails. This is the first time I'm using CoffeeScript and I'm also fairly new with Rails too, so I don't know how to make this simple CoffeeScript function to work in the right way. Below is my people.js.coffee file at app/assets/javascript directory.
myFunction = ->
alert "test"
The alert “test” message only shows up when I load the page (app/views/people.html.erb and _form.html.erb partial); not when I click the below button in the form:
<%= submit_tag "Test CoffeeScript", :type => 'button',
:id => 'coffeeScript', :onclick => 'myFunction()' %>
I don't know why this strange behaviour is happening. Why :onclick is not working? The generated source code for the button should be ok:
<input id="coffeeScript" name="commit" onclick="myFunction()"
type="button" value="Test CoffeeScript" />
Below is my application.js file at app/assets/javascript.
...
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
My Ruby version is 2.1.0
Here are some values when I ran bundle show command:
coffee-rails (4.0.1)
coffee-script (2.2.0)
coffee-script-source (1.7.0)
jbuilder (1.5.3)
jquery-rails (3.1.0)
rails (4.0.2)
railties (4.0.2)
sass (3.2.19)
sass-rails (4.0.3)
sprockets (2.11.0)
sprockets-rails (2.0.1)
turbolinks (2.2.2)
My JavaScript files are working ok; I tried similar type of function in JavaScript and it was ok.
It's because the function is in another scope. You can do it either with unobtrusive jquery.
$ ->
myFunction = ->
alert("test")
$("#coffeeScript").on "click", ->
myFunction()
And remove the onclick handler from
<%= submit_tag "Test CoffeeScript", type: 'button', id: 'coffeeScript' %>
or...
root = exports ? this
root.myFunction = () -> alert("test!")
I'd recommend the first one, but that is just my opinion though. Also, you have to be very careful about indentation in CoffeeScript, it works a lot like Python, whitespace matters.
An explanation on scopes and js/coffee is here How do I define global variables in CoffeeScript?
Related
I have a Rails 4.2 framework and use Kaminari to paginate. Everything works great, but I want to test everything with Capybara, thus I run into the page conflict. So I wanted to rename the page_method_name of Kaminari and followed the guide from them:
$ bundle exec rails g Kaminari:config
This results in this file
app/config/initializers/kaminari_config.rb
Kaminari.configure do |config|
config.page_method_name = :plant
end
In which I simply uncommented the config.page_method_name and set it to :plant (as an example from the Kaminari docs to avoid any reserved method name conflicts for something like :kaminari_page).
Then I adjusted the appropriate controller to
def index
#q = Interaction.published.order(updated_at: :desc).limit(200).ransack(params[:q])
#selection = #q.result
#interactions = Kaminari.paginate_array(#selection).plant(params[:page]).per(10)
respond_to do |format|
format.html
format.js
end
end
I restarted everything and got this error when visiting the
undefined method `plant' for #Kaminari::PaginatableArray:0x00005568fb42ba30
Gemfile.lock:
kaminari (1.2.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.1)
kaminari-activerecord (= 1.2.1)
kaminari-core (= 1.2.1)
kaminari-actionview (1.2.1)
actionview
kaminari-core (= 1.2.1)
kaminari-activerecord (1.2.1)
activerecord
kaminari-core (= 1.2.1)
kaminari-core (1.2.1)
I was then googling around and also added require 'kaminari' (which does not make so much sense, because I did not get the uninit constant error) and also included the Kaminari configure section into the class Application < Rails::Application inside config/application.rb.
Nothing worked, always the same error.
You show your files location as app/config/initializiers/kaminari_config.rb. If that is the actual location of your file it fully explains why the method doesn't exist. The location should be app/config/initializers/kaminari_config.rb. With the location you specified it wouldn't be automatically loaded when the app starts.
If the location you showed is just a type then that wouldn't be the cause, but if you didn't include Capybara into the global object, which you shouldn't be doing anyway, then you wouldn't have a name collision. Current versions of Capybara issue a warning when you do include it into the global object specifically for this reason.
Also is there any reason you're grabbing all the results and then paging them, rather than using the page (or plant) and per scopes that would be defined on Interaction?
I'm upgrading a rails app from 4.2 to 5.0. The ruby version is 2.6.4
The app has a view that allows the user to click a link to run a scheduling script that assigns students into courses/classes. The script can take several minutes to run to completion, so we show a modal dialog box that, like a progress-bar, shows where the process is currently at. But in the rails 5.0 version, this is not working. There is no error(s), just the popup is not appearing. I figured it was a JQuery thing, so using try/catch blocks, I found it is in the .dialog(...) call. But I'm not sure why it is failing. I believe I have the correct JQuery gems and the assets/files set up correctly for rails 5.0.
Gemfile:
source 'http://rubygems.org'
gem 'rails', '~> 5.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
...
Gemfile.lock:
...
jquery-rails (4.4.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (6.0.1)
railties (>= 3.2.16)
...
app/assets/application.js:
...
//= require jquery-ui
//= require jquery
//= require jquery_ujs
//= require jquery-ui/widgets/dialog
//= require_tree .
app/assets/stylesheets/application.css
...
*= require_self
*= require_tree .
*= require jquery-ui
*= require jquery-ui/dialog
The popup form:
app/views/elements/_scheduling_dialog_popup.html.erb:
<%
# This partial will attach a Jquery event to a submit button that will display a modal-screen
# during the (potentially) long scheduling process.
%>
<% content_for :head do %>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a#autoschedule").click(function() {
JQuery('div#import_modal_box').dialog({bgiframe: true, modal: true, width:450, draggable: false, closeOnEscape: false, resizable: false});
});
});
</script>
<% end %>
<div id="import_modal_box" style="display:none;">
<p style="float:left;margin-right:1em;"><%= image_tag 'ajax-loader.gif' %></p>
<h3>Auto Class Assignment for <%= pluralize(Student.count,'student') %></h3>
<p>There are <%= pluralize(ClassTime.sum(:enrollment_limit),'spot') %> available in <%= pluralize(ClassTime.count,'class') %> for <%= pluralize(UpcomingCourse.count,'course')%>.</p><p>This may take a few minutes, please wait.</p>
<p> </p>
</div>
I've read through the docs at: ruby-docs. I see I only need to use //= require jquery-ui to require everything , but I added the //= require jquery-ui/widgets/dialog just to be sure.
What am I missing to get this to work?
Thanks for any advise.
I was "finally" able to resolve this issue. It actually had nothing to do with the dialog() function, directly. I brought up the Developer Tools and ran the Console tab/window to see if I was getting any errors or warnings and found I had a ton of ReferenceError: jQuery is not defined .... errors. After some searching I found that Rails 5.2 does not need jquery_ujs, so I removed the //= require jquery_ujs line from my assets/javascripts/application.js file. Now it works.
This is the error generated whenever I try to do a simple page reload:
Completed 500 Internal Server Error in 129ms
** [Raven] couldn't find file 'turbolinks'
(in /hj-project/app/assets/javascripts/comfortable_mexican_sofa/admin/application.js.coffee:2) excluded from capture due to environment or should_capture callback
ActionView::Template::Error (couldn't find file 'turbolinks'
(in /hj-project/app/assets/javascripts/comfortable_mexican_sofa/admin/application.js.coffee:2)):
6: = stylesheet_link_tag 'comfortable_mexican_sofa/application'
7: = stylesheet_link_tag 'comfortable_mexican_sofa/admin/application'
8: = javascript_include_tag 'comfortable_mexican_sofa/application'
9: = javascript_include_tag 'comfortable_mexican_sofa/admin/application'
10:
11: = yield :head
12: = cms_hook :html_head
sprockets (2.2.3) lib/sprockets/context.rb:102:in `resolve'
sprockets (2.2.3) lib/sprockets/context.rb:142:in `require_asset'
Here is the app/assets/javascripts/comfortable_mexican_sofa/admin/application.js.coffee file:
//= require turbolinks
//= require tinymce
//= require cocoon
//= require jquery.address
//= require_self
//= require_tree .
window.CMS.wysiwyg = ->
tinymce.init
selector: 'textarea[data-rich-text]',
plugins: 'advlist autolink lists link image preview hr anchor pagebreak searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime nonbreaking save table contextmenu directionality paste textcolor',
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link | image uploadimage",
image_advtab: true,
From the Gemfile.lock:
comfortable_mexican_sofa (1.8.5)
turbolinks (5.2.0)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
DEPENDENCIES
comfortable_mexican_sofa (~> 1.8, >= 1.8.5)
turbolinks (~> 5.2.0)
Rails 3.2.
What could be causing this?
It seems the issue was the version of Turbolinks I was using.
Turbolinks 5 doesn't seem to work with Rails 3.2.
So once I did this in my Gemfile:
gem 'turbolinks', '~> 2.5', '>= 2.5.3'
And then did a bundle install and/or bundle update it works like a charm now.
I want to use bootstrap in my project. My operating system is Windows, Ruby version is 1.9.3, Rails version 4.0.4.
I try to bundle one gem every time because there is dependence in some gem when I execute bundle install.
bootstrap-sass (2.3.2.0)
sass (3.3.5, 3.2.19)
sass-rails (4.0.2)
sprockets (2.12.0, 2.11.0)
Now, I add //= require bootstrap into application.js and add #import "bootstrap" into application.css.scss. But when I start server, I got Load Error in *Controller#home:
cannot load such file -- sass (in D:/first_app/app/assets/style sheets / application.css.scss
Extracted source (around line #5):
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
Rails.root: D:/rails_projects/first_app
Any advice?
Did you make sure that you also removed the sprockets //= require directive in your application.css.scss file?
Also, make sure all your gem versions are aligned. According to this rubygems page, version 2.3.2 of boostrap-rails requires version 3.1 for the saas gem. Running bundle install should also notify you of any gem version mismatches in your Gemfile.
Finally, if you're using Rails 4, why are you using an older version of bootstrap-rails? The latest stable version of bootstrap-rails is 3.1.1.0 (as of Feb 13, 2014).
I'm trying to use Sass engine in a controller like in this blog post. But Rails (2.3.4) ActiveSupport can't load it because of an error:
ArgumentError (Anonymous modules have no name to be referenced by):
haml (3.0.25) lib/sass/script/color.rb:19
haml (3.0.25) lib/sass/script/literal.rb:10
haml (3.0.25) lib/sass/script/string.rb:1
haml (3.0.25) lib/sass/script/operation.rb:2
haml (3.0.25) lib/sass/script.rb:5
haml (3.0.25) lib/sass/engine.rb:22
app/controllers/stylesheets_controller.rb:1
Here's the code in Sass::Script::Color
class << self; include Haml::Util; end
I've tried with the latest Haml version and also version 2.2.24 with the same result. Is there a compatible version? Is there a problem on my side?
Finally it was quite stupid, the Sass::Script::Color file wasn't requiring Haml::Util, so I had to do it myself:
require 'haml/util'
require 'sass/engine'