Destroy Function in Ruby On Rails does not work (Another thread) - javascript

before I opened this post I read a lot of post on stackoverflow and I tried the solutions...
from:
Ruby on Rails: Why the confirmation message does not appear in "link_to('delete', ...)"?
and
Ruby on Rails: Why the confirmation message does not appear in "link_to('delete', ...)"?
I'm really confused because following the tutorial on the book
https://www.railstutorial.org/book/toy_app
but I have the other version where the toy_app is called demo_app
I don't know if it is a problem of compatibilty but almost every step I do I need to research to solve one configuration error... you know, change sqlite3-ruby for sqlite and so on...
Now I'm on the same problem of others, Destroy Function in Ruby On Rails does not work, I tryed various solution and I canĀ“t make it.
My configuration is:
Windows 7 64 bits
Rails 3.2.19
Ruby 1.9.3
HTML destroy code is:
<td>Destroy</td>
and my application.html.erb is
<!DOCTYPE html>
<html>
<head>
<title>DemoApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag 'jquery_ujs' %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
application.js looks like
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
= require jquery
= require jquery_ujs
//= require_tree .
And I checked Chrome and Internet Explorer and on both of them Javascript is enabled...
What more can I do??
Thanks

Related

Ruby on rails javascript doesn't load properly (HTML5 template)

I am very new in ruby, so please don't judge me too much :)
I was trying to make this template http://freebiesbug.com/code-stuff/sedna-one-page-website-template/ run on rails. And it went pretty well.
However, I have stuck on configuring javascript parameters (the way it loads on rails). I am not sure how to do that correctly. Things that slides in the template doesn't work for me, like apple images and slider of employees in the bottom...
I tried to include all file names in javascript.rb file and also I have used:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
but non of these worked.
Any ideas where I may made a gap?
Thanks!
Javascript files end in .js, not .rb, so thats one problem.
The line
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
tells rails to package and include the application.js file found at app/assets/javascripts/application.js. Assuming your template has one associated javascript file, you can copy and paste the contents of that file into your application.js file (which may be the easiest solution). You can also add any javascript (or coffeescript) files to the app/assets/javascripts folder, and those files will be packaged and served up in the default configuration of rails.

Rails: couldn't find file 'typeahead.js'.. until i remove 'data-turbolinks'

I installed Elasticsearch (with searchkick gem) following this tutorial - https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch.
Search is working and now I'm implementing autocomplete function with typeahead.js which I installed using Bower.
However I get the error message:
Sprockets::FileNotFound at /books couldn't find file 'typeahead.js'
I tried to include it in various orders in my assets file, without luck..
However I realized removing turbolinks from application.html.erb solved the problem.
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
This does not seem right... How can I resolve this?
I had this exact same problem. The author named the library with the .js extension added and I assume it has something to do with application.js ignoring the .js part at the end. I had this problem when using rails-assets and once I poked around more in the gem source I saw two .js.js extensions added.
You can add it as so:
//= require typeahead.js.js
Then you can re-enable
'data-turbolinks-track' => true

Rails assets not available on production

I have this specific issue. On a (LARGE) rails setup I have a backbone project in /app/assets/reader/. All of my javascript assets are precompiling dynamically into reader.js, this works fine. My i10n files in locale/ don't play nice however because they don't need any precompiling. In development it works fine, but in production they are not available.
In my /app/views/layouts/reader.html.erb file I have the following lines:
<%= javascript_include_tag "reader" %>
<%= javascript_include_tag "locale/en" %>
The problem is that the lower one results in a 404 error on production.
I've tried the following alternatives:
<%= javascript_include_tag "en" %>
<%= javascript_include_tag "locale/en" %>
<%= javascript_include_tag "reader/locale/en" %>
None of these seem work. The last one even broke on development.
PS: in applicaton.rb I have:
...
config.assets.precompile += [
...
'reader.js',
...
]
...
config.assets.paths << File.join(Rails.root, 'app', 'assets', 'reader', 'locale')
You removed all files in public directory folder then restarted server? Also inside of your js folder you have a folder
reader/locale/en.js
? Double check all paths and if it works in development you should check out your error log in production to see where the problem is coming from.
The solution was not in the javascript_include_tag, but rather in the way config.assets.precompile was formatted.
By default Rails scans for any subfolder DIRECTLY within assets. That meant that the locale file had to be added as locale/en.js to config.assets.precompile, and that the config.assets.paths line was not even necessary at all.
The way rails scans subfolder is really specific and important. Get that right, and it should all work like a breeze. Once you know how it works, it actually gets quite powerful.
Pro tip:
I ended up using locale/*.js in combination with <%= javascript_include_tag "locale/#{I18n.locale}" %>, as I actually have a lot of locale files.
Had the same issue, turns out there is a line in config/environments/production.rb:
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
which didn't allow Rails to serve static files, including the precompiled assets. Changing it to
config.public_file_server.enabled = true
fixes the issue.
This is caused by the fact that Nginx and Apache handle static serving on their own. But Rails is configured to use Puma by default so this doesn't really make too much sense for it to default to these settings.

Can't edit records using best-in-place gem for Rails 3.0

I'm trying to get the best-in-place gem to work on a Rails 3.0 app but nothing seems to work. I can't click on any of the objects to edit them.
This is what I've done so far (following the GitHub instructions from the link above as well as the RailsCast:
In my Gemfile:
gem 'rails', '3.0.11'
gem 'best_in_place', '~> 0.2.0'
I also ran rails g best_in_place:setup since I'm on 3.0 and it generated a best_in_place.js file into public/javascripts.
And finally to display the records to be edited in-place:
<div class="profileGains">"<%= best_in_place #project, :my_quote %>"</div>
I'm using RESTful routes so I have the update action for my project resource.
At this point, the record is displayed but I can't click on it (or any of the other ones).
Any suggestions? I'm new to JS and really stuck.
EDIT: I created an application.js file in which I added the following code:
//= require jquery
//= require jquery.purr
//= require best_in_place
$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});
I also tried the suggestion in the comments by Richlewis to add a script tag in my view:
<script type="text/javascript" src="/public/javascripts/best_in_place.js"></script>
Finally, I tried changing <%= javascript_include_tag :defaults %> to
<%= javascript_include_tag :all %> in my application.html.erb layout file.
And again, best-in-place doesn't work.
UPDATE: When inspecting the element using Chrome I found this error displayed in the logs:
Uncaught TypeError: Object [object Object] has no method 'best_in_place'
application.js line 7
Any idea what this means?
Answering my own question as I figured it out. The best_in_place.js file wasn't being rendered in my view because this code //= require best_in_place only works for apps with an asset pipeline.
I had to explicitly declare the best_in_place.js file in my application layout file. That did the trick.

Javascript doesn't work for javascrip_inlcue-tag Rails 3.2.2

I'm following the demo_app in Michael Hartl's book Ruby on Rails 3 Tutorail. The demo is a simple Scaffold User name:string email:string
I'm getting an error in my application.html.erb file:
Errno::EINVAL in Users#index
Showing ~/demo_app/app/views/layouts/application.html.erb where line #6 raised:
Invalid argument - cscript //E:jscript //Nologo //U /tmp/execjs20120323-4388-1an85xw-0.js 2>&1
(in ~/demo_app/app/assets/javascripts/users.js.coffee)
3: <head>
4: <title>DemoApp</title>
5: <%= stylesheet_link_tag "application", :media => "all" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
if I chnage line #6 to:
<%= javascript_include_tag "default" %>
The program will run perfectly except it won't allow me to delete users (presumably because it's not running the javascript).
Environmental info:
$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [i386-cygwin]
$ rails -v
Rails 3.2.2
Content of ~\demo_app\app\assets\javascripts\users.js.coffee:
# 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://jashkenas.github.com/coffee-script/
Content of ~\demo_app\app\assets\javascripts\application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
try to uncomment gem 'therubyracer' and run bundle, and also it's better to use last stable version of ruby
I searched around for other questions that discussed the gem therubyracer. I found one answer that suggested what Said Kaldybaev had suggested here. That didn't work for me. But there was another suggestiong to remove the code
//= require_tree .
from the app/assets/javascripts/application.js file.
Deleting that code made the application work (including the destroy function).

Categories