Rails 4 Route "match" changes the CSS/JS base URL on template - ruby-on-rails

I'm making a blog system for my Rails application and I'm stuck with this problem a while and don't know what's going on with my routes.
I installed a template for my app and all the css/js/images files from this template are in "public/" not in the assets folder. It was the only way I found to make the template working.
My blog system have this routes:
When I access "/blog" it serves the index view and it loads all the assets from "assets" and "public". But when I try to access "/blog/" or the matched route "/blog/category_slug/post_slug" rails tries to load the files from "public/" with this URL:
"base_url/blog/category_slug" and that's really weird!
I'm current using Rails 4.0.2. Any thoughts?

Fixed the problem adding a "/" before the path to the assets:
From: "css/bootstrap.css"
To: "/css/bootstrap.css"
I still don't know why this only happens in the blog controller, but that worked.
Thanks.

Related

Rails assets controller serves public assets instead of assets' index page

In my Rails app, I have an assets controller (Since the requirement of the model name is Asset).
I use
resources :assets
to route my controller to this URL to display /assets.
Everything works fine in the development mode. However, after I deployed the application on AWS Beanstalk, it displays Error 404 from Nginx.
After going through Nginx error log file in /var/log/nginx/error.log, I found this.
open() "/var/app/current/public/assets" failed (2: No such file or directory), client: 172.31.21.101, server: _, request: "GET /assets?status=active HTTP/1.1"
What I understand from the log, it means the application tries to look for assets /var/app/current/public/assets instead of GET /assets route.
Therefore, my question is, how can I make the application serve the assets folder the view file? Or is there any workaround? One solution I can think of is changing the assets folder to something else.
It's a name collision of the routes you're creating.
As of default, rails assets are stored in public/assets. Rails then tries to use xsendfile (if you have it correctly configured) to deliver the given asset from the public directory.
To avoid these collision, you can either change your RESTful route to a name other than :assets or you can customize the asset prefix with this code:
# config/application.rb
config.assets.prefix = '/some_other_path'
EDIT: I highly recommend to rename the RESTful route, because some poorly written gems do not use assets.prefix but the hardcoded /assets path. Nested routes could be also fine if it is possible. (Thanks to Gabor Garami)

Ruby Rails - Cant locate routes.rb to add page to site

I am currently working on a project where the majority if the site structure is already in place. I am trying to add a new section to it but am having difficulty getting links to work.
I replicated what another page had however in tutorials I am seeing reference to a routes.rb file which specifies how links work. The problem is i cannot locate the routes.rb file. Is there anything I am missing or is routes.rb specific to a certain version and I am using a different version. Any help would be much appreciated.
I think your routes file was deleted. You can check git status. You will be find deleted file there. Rollback that change or create new file routes.rb in config/ folder.
If the app is a standard Rails app, you should be able to find routes.rb in the app's config/ folder.
How about just using your editor to do a file search and locate the file?
For Sublime Text, cmd + p for MAC will suffice.

Can I Identify The Environment Using HTML Code?

I just figured out how to create custom error pages in the public folder using I18n. Those error pages have links that will return the user to a page in the application such as the home page.
When my application was running 3.2.13 I had the custom error pages in the app folder using config.exceptions_app = self.routes in application.rb. When I had that code all I had to do was use a link_to statement pointing to the route I wanted the user to return to. If I was in the development environment it would go to http://localhost:3000/somelink and in production it would go to http://myrailsapp.com/somelink. I replaced these with the ones in the public folder after rewriting them in Rails 4 because it appears that config.exceptions_app = self.routes is ignored in Rails 4.
My error pages in the public folder are html, not html.erb so there is no ruby/rails code. I would like to replicate what I had previously where I can check the environment in my error pages and point to localhost for development or my domain URL for production. I currently have code in my error page similar to this:
My Link Text
I'm definitely open to changing these to html.erb files. I initially thought about that but from my research and trying what I found nothing is working for Rails 4.
Any help would be appreciated.
You don't need any "autodetection" in html, just cut the domain part from the url and use a local path instead.
Just change http://myrailsapp.com/en/home to /en/home.
And yes, you do need the opening slash to be independent from the current_url in your link.

RoR link_to and assets paths

I have a RoR (1.9.3 - 3.2) application running on IIS 7.5 over the HeliconZoo Module.
It is located in a sub directory like http://server/application/
Now if i use link_to with a direct location like <% link_to "/users" %> it is linked back to http://server/users instead of http://server/application/users (while linking to a controller action works fine).
Also (which is the bigger problem here) the application is precompiling all assets into server/application/public/assets but is searching for them in server/public/assets when rendering.
If i set config.assets.prefix the assets are also being rendered to a different location ("/application/..." as addition everytime) which results in a constant desync and assets are never being loaded.
I can workaround this by changing config.assets.prefix to "/application/public/assets" for precompile and changing it back to default after, but this is pretty annoying.
Is there some kind of config to tell the render to add a prefix ("/application") on direct links and assets?
I've never used either IIS or HeliconZoo for Rails deployment, so this might not work, but I'll take a crack at answering anyhow, as it seems to me what you're really trying to do is get Rails to understand you're deploying it to a subfolder, and that isn't too difficult. Simply encapsulate everything in your routes.rb inside a scope, like so:
scope "/context_root" do
resources :controller
resources :another_controller
match 'welcome/', :to => "welcome#index"
root :to => "welcome#index"
end
I copied this answer directly from here, which might provide more useful information to you.
Please try and open IIS Manager, navigate to the “application” folder, open context menu and select “Convert to Application”. Normally no specific tinkering with Ruby code required.

Rails 3.1 sub uri assets path issue

I am currently using rails 3.1.0.rc1 and when i deploy it to a server which is using passenger and it is deployed to a suburi.
But when I go into look at the site, path to the style-sheets and java-script files are not being included because of the path.
Can someone let me know how to specify the path in the environments so that all the assets (images , style-sheets and java-script ) point to the right path ?
This issue was fixed on Rails 3.1.0.rc4. You no need to specify anything for the config.assets.prefix unless you want the change the default /assets.
I got around it using YourApp::Application.config.assets.prefix = "/suburi/assets". Acording to issue #1489, helpers didn't respect this till last Tuesday, so you'll have to use 3.1rc4. Url's seem to get created correctly, so it may be more of a phusion problem.

Resources