Using paths in javascript assets in rails 3.1.rc1 - ruby-on-rails

I have a file called myjavascript.js.erb in my assets path. This is where I put all my project related javascript etc.
As I understand it, rails runs this file through the erb interpreter first and then loads the resulting JS file.
I have the following line in my file
console.log( "<%= root_path %>" );
I was hoping that this would log the root path of the project but unfortunately it seems to only get me
"/path to rails project omitted/app/assets/javascripts"
Surely this should point to the root of my project? Am I doing something wrong?

You can use
Rails.root
To get to the root path in Rails.

Related

Why Rails 5.0 on Windows 7 with RailsInstaller is rendering index.html.erb view from the gems directory?

I'm using Rails 5 on Windows 7 by RailsInstaller. I'm following RailsGuides to build the blog.
When I do change the index.html.erb file to display <h1>Hello Rails</h1>, I get the same (default, vendor provided) view result.
Puma server gives me this log line:
Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/templates/rails/welcome/index.html.erb
Any help for what's happening?
You cannot serve .erb files out of public. The public directory is for static files.
If you want a request for index.html to be served out of public, your file must be called index.html.
In your config/routes.rb you need to create the default root. For example:
root to: "welcome#index"
Where: welcome is the name of the controller and index is the name of the view.
Regards

Angulars + Rails: ngInclude directive not working

I'm working on a rails + angularjs app.
Here is my file structure:
app/
assets/
javascript/
templates/
home.html
partial.html
Inside the home.html.erb file I want to include the partial.html.erb file.
home.html.erb
<ng-include src="'partial.html'"></ng-include>
I also tried
<ng-include src="'<%= asset_path('partial.html') %>'"></ng-include>
But still doesn't work... Thanks for your help
Setup gem "angular-rails-templates"
It will automaticaly compile your templates into javascript, and make them available to angular. After that you may use ng-include as usual.
<ng-include src="'templates/partial.tpl.html'"></ng-include>
It's not a good idea to mix server templates and AngularJS' templates. Put your AngularJS templates in your public directory, then put in the src attribute the path to this template from the client.
public/templates/partial.tpl.html => <ng-include src="'/templates/partial.tpl.html'></ng-include>"
Another way to get the template from the client is to compile your templates to a JS file with html2js for example.

How to write path redirect in ruby on rails and angularjs

Sorry that I am new to ruby on rails.
I am trying to create a directive in angularjs.
What I was doing is create an html file in folder view/forms, named topRight-buttonGroup.html
Then I just created a simple directive for test:
app.directive('topRightButtonTools', function(){
return {
restrict: 'C',
templateUrl: 'topRight-buttonGroup.html'
};
});
But getting the error message in the console is:
GET http://localhost:3000/forms/topRight-buttonGroup.html 404 (Not Found)
Am I missing something should be done on ruby on rails? Or should I write some redirect code and save in somewhere?
my full folder structure is:
If you need the form to be accessible so, create a folder inside public/ as forms/ and have the file placed there. Only content inside the public dir are rendered directly.
Unlike core php or other such scripting languages, RoR requires controllers to render actions which arent merely files that are interpreted. Also, routes are to be defined in config/routes.rb.
Refer:
http://guides.rubyonrails.org/routing.html
http://www.xyzpub.com/en/ruby-on-rails/3.2/statische_webseiten.html

rails 3.1 coffeescript file extension problem

I have this stock rails 3.1 app, before even adding anything, I was testing to see if the assets are working as advertised
so i created this app/assets/javascripts/test.coffee
where test.coffee is just a
alert "hi"
When I navigate to http://127.0.0.1:3000/assets/test.coffee, I do see
(function() {
alert("hi");
}).call(this);
But if I do http://127.0.0.1:3000/assets/test.js, I get routing error; but I thought this is the correct behavior, not the above one. What have I done wrong?
Try renaming your your js file to test.js.coffee
Coffee extension indicates that this file should be preprocessed with coffee processor, same as:
index.html.haml is just index.html with haml processor or
style.css.sass is just style.css with sass processor

How do I use CSS with a ruby on rails application?

How do I use CSS with RoR? When I link externally, I'm never able to see the files. I cp'd the .css file to every folder I could think of...views, controller, template, and nothing seems to work.
What do I need to do to enable external CSS files with a rails application? I'm new to rails, so forgive me if this is basic.
Put the CSS files in public/stylesheets and then use:
<%= stylesheet_link_tag "filename" %>
to link to the stylesheet in your layouts or erb files in your views.
Similarly you put images in public/images and javascript files in public/javascripts.
If you are using rails > 3 version, then there is a concept called asset pipeline. You could add your CSS to
app/assets/stylesheets
then it will automatically be picked up by the app. (this is useful as rails will automatically compress the CSS files)
read more here about the asset pipeline
Use the rails style sheet tag to link your main.css like this
<%= stylesheet_link_tag "main" %>
Go to
config/initializers/assets.rb
Once inside the assets.rb add the following code snippet just below the Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( main.css )
Restart your server.
I did the following...
place your css file in the app/assets/stylesheets folder.
Add the stylesheet link <%= stylesheet_link_tag "filename" %> in your default layouts file (most likely application.html.erb)
I recommend this over using your public folder. You can also reference the stylesheet inline, such as in your index page.
The original post might have been true back in 2009, but now it is actually incorrect now, and no linking is even required for the stylesheet as I see mentioned in some of the other responses. Rails will now do this for you by default.
Place any new sheet .css (or other) in app/assets/stylesheets
Test your server with rails-root/scripts/rails server and you'll see the link is added by rails itself.
You can test this with a path in your browser like testserverpath:3000/assets/filename_to_test.css?body=1
To add to the above, the most obvious place to add stylesheet_link_tag is in your global application layout - application.html.erb.
With Rails 6.0.0, create your "stylesheet.css" stylesheet at app/assets/stylesheets.
Have you tried putting it in your public folder? Whenever I have images or the like that I need to reference externally, I put it all there.

Resources