I have a JavaScript file called users.js and it is in app/assets/javascripts/users.js. I have it load in a single page using
<%= javascript_include_tag params[:controller] %>
I was thinking of using some Ruby code in the JavaScript file, so I changed the filename to users.js.erb. But when I do, the file shows up in my SublimeText project view as users.js.erb.js, and when I run my Rails app, I get a 404 error saying users.js.js couldn't be found.
Help? Thx.
From my perspective, this is not a rails problem, but a problem with the editor you use. Check the file information in finder with cmd-i and change it back to users.js.erb.
Related
Im building a application where I give my clients a .js file so they can include into <head></head> and that .js file is providing some functions to my clients website.
Basically the .js file is the path to show.js.erb view. I am looking for protecting my code and looking for a way so clients/users can't just load the http://localhost:3000/js_files/2.js path on their browser and see the code.
Every client has different ID (.js path) and different website and I can detect that from my db. Is there a way so I see if the file/path is loading from my clients website and if Yes load/show the js code, otherwise not showing anything.
In other words, a way to check if file is loaded on for ex: mydomain.com or if they just copy the path into their browser.
I have read about obfuscate, but its not useful in my case.
You can use request.base_url and getting users domain name from database.
request.base_url helper:
Basically request.base_url, takes only the domain name and removed all other parts.
ex: http://domain.com/q/45/rails => domain.com
Here is my code:
<% if request.base_url == #domain.website %>
// JS CODE
<% end %>
If I understood it correctly, you want to check if your clients copied the js file on their assets or if they are using your domain on the script SRC attribute.
You think something like this may work?
Add a XHR get/post to your domain on your JS
Your domain receives the request and process the referer of the XHR (you can check on your controller on rails through request.env["HTTP_REFERER"])
I have an interactive header for my site located in /views/layouts/_header.html.haml.
I want all the JavaScript for the header to be collected into a single file. Most importantly, I'm using Twitter Typeahead and Bloodhound for a search field with auto-suggest. A lot of this JavaScript has to be run after the header is rendered, so its inclusion in application.js (which is included on my page in application.html.haml) doesn't work, as this runs before the header is rendered.
I added search-bar.js under /assets/javascripts/ which contains all the JS I need to run on this page.
At the bottom of my _header.html.haml I just linked with a regular script tag.
%script{src:"/assets/movie-search-bar.js", type:'text/javascript'}
This works fine locally, but on my dev server I get a 404 for that asset. Is it possibly throwing out the static file for performance reasons? Even if it does work, by having it included in the asset pipeline, wouldn't the script be loaded twice (once as an individual script, and once in application.js)?
I'm getting the impression that this is not the best way to isolate the javascript of a partial into its own file. What is the best and most "railsy" way to ensure that the script is loaded once, after _header is rendered, and isolated within its own JavaScript file?
EDIT: Could this perhaps be as simple as moving the following lines to the bottom of my footer partial? Is this considered good practice in Rails?
#{ stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true }
#{ javascript_include_tag 'application', 'data-turbolinks-track' => true }
You can use javascript_include_tag on scripts other than application.js. First, make sure your script is in app/assets/javascripts folder: app/assets/javascripts/movie-search-bar.js.
Then use the javascript_include_tag helper in your view:
javascript_include_tag 'movie-search-bar'
And tell Rails to precompile that asset.
config.assets.precompile << "movie-search-bar.js"
Well I have done this task .If you want to include a particular javascript in your view .First create a folder in your views folder .For example page_js and then create some partial like _file.html.erb .And place your javascript into it .And then render that file wherever you want .So your javascript will work for that particular page only .If it doesn't work then tell me .
I am completely new to Ruby on Rails and I'm trying to serve an application in which an html file calls javascript that is located in a separate file. I placed the javascript file in the javascripts fold of my rails application. My html displays fine but when I press a button that is intended to call a javascript function I receive an error.
Uncaught TypeError: Cannot call method 'myMethod' of undefined
I can see that the file is present by looking in the resources tab of google chromes developer tools. It also looks like rails is correctly including the javascript file in the html file.
<script data-turbolinks-track="true" src="/assets/myScript.js?body=1"></script>
I'm not sure what I'm doing wrong. Any help would be greatly appreciated.
You should really use the javascript_include_tag helper to keep the app to convention:
<%= javascript_include_tag "myScript" %>
The error is to do with your Javascript's call to myMethod. The error will be caused by referencing myMethod on an object / var which doesn't exist
I have a js file called create.js.erb that is in my view folder. It's supposed to be called when I try to create a record, but it isn't being called. I can't figure out why, and to be totally honest, don't even know how my app calls a js file in the view folder, so I'm not sure what code to paste here to help debug the problem.
Can anyone explain to me how js in a view folder is executed, and when I would want to put a js file in my view folder instead of in the asset pipeline?
*.js.erb files are rendered when you are using AJAX/JS with your controller actions. By default, when you call the create method, Rails will respond using HTML. This will load a new page. Sometimes you want to use AJAX instead, and that's why you create js.erb files in the view folders.
For this to work, the form and/or link_to objects you are using must be AJAX enabled (they should have a :remote => true attribute on them) If they are not specified as remote forms, they will execute the HTML instead of the JS and the create.js.erb file will never be called.
Your controller method also needs to know how to respond to js requests. You need something like:
respond_to do |format|
format.js
end
That code tells Rails to look for a file called "method".js.erb in your view folder, so in this case, create.js.erb.
These files are completely different from regular JS files you put in the asset pipeline -- these are view templates to be rendered as the result of a controller action.
You might find some Rails/AJAX tutorials helpful...here's a pretty good one that walks you through this whole process:
http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3
Hope that helps, if you need more assistance please post the code for your controller and any of the view files...
I'm trying to get a javascript item displaying on Heroku Rails 4 applicarion. On my local development machine, everything is running perfectly. Can't get it to work on Heroku.
In my application.html.erb file I have added:
<%= javascript_include_tag "application" %>
and in another view file (events.html.erb) I have:
<script type="text/javascript">
$(document).ready(function()
...code to add data to a timeline using getjson()...
</script>`
which is referring to a timeline.js file in the assets>javascripts folder. When an event is added (using Rails CRUD operation, the javascript code is calling a getJson() method and is adding the event to a timeline. This is working as expected on local machine.
When I push to Heroku, I can add events through rails, but the timeline is not displayed.
My initial idea was that Javascript was not being loaded on Heroku, so therefore I added a simple:
<script type="text/javascript">
alert('Displays OK')
</script>
call and this displays just fine on Heroku (as well as on my local machine).
When looking at the page source on Heroku, I can see:
<script src="/assets/application-6da332d35efc3706e98f87ead9900f20.js">
so it appears that the timeline.js file is precompiled (it seems a minimized version of the timeline js).
On my local machine, I don't see the precompiled js file, but I see:
<script src="/assets/timeline.js?body=1"></script>
<script src="/assets/timeline.min.js?body=1"></script>
The fact that on Heroku, it displays the javascript alert, but not the timeline, seems to indicate that something is messed up with the javascript code in the precompilation.
I can't see anything obvious in the heroku logs. It says slug compilation was finished successfully and I can't see error messages.
Is there a way I can enforce Heroku to not use the precompiled js file, so I can have on Heroku:
<script src="/assets/timeline.js?body=1"></script>
<script src="/assets/timeline.min.js?body=1"></script>
I have the following in production.rb file:
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.compress = true
config.assets.digest = true
config.assets.debug = false
Hope someone can help me on this. This has been bugging me for couple of days now. Any help appreciated.
Thanks!
I think you need to specify that timeline.js must be precompiled.
On config/environments/production.rb, add this :
config.assets.precompile += %w( timeline.js )
In you view, include you timeline js with :
<%= javascript_include_tag "timeline" %>