RoR link_to and assets paths - ruby-on-rails

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.

Related

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

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.

How do I customize the URL path to 'subpaths' for the assets pipeline?

I notice that everything regarding Rails and Sprockets falls into the 'domain/assets/' URL, but I have this issue with certain files that are trying to get CSS and image files from:
somedomain/assets/css/{filename}
somedomain/assets/images/{img_name}
I'm trying to find some way to split up the paths of those assets so they don't all go into the same /assets path so that the URLs work. I thought maybe I could either do that in the configuration, but I didn't find anything other than renaming config.assets.prefix, or the routes.rb, because I thought something like "get /assets/css" would work but I don't know where to point it to.
In any case, at this point I'm stuck (btw, this has been in development. in production, I'm running into somewhat of a different issue where the javascript_include_tag is trying to go to /javascripts path).
I did this in my application.rb in config:
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/css"
Got the answer. Sorry if my question and examples weren't clear enough... I'm always terrible at it...
Anyways I took a look at the sprockets documentation (not the sprockets-rails docs) and this is what I did (modified config.ru):
map '/assets/images' do
environment = Sprockets::Environment.new
environment.append_path 'app/assets/images'
run environment
end
map '/' do
run Rails.application
end
Unfortunately, I don't fully understand what's going on in the config.ru other than the fact that the entire application starts here but it looks like that mapping lets me serve it up in this way (Surely, on something like AWS I can use nginx but for Heroku and overall, this seems to make things easier).

Rails/Gollum: Gollum does not use path prefix to load assets

I mount gollum inside my Rails app like provided this answer.
It works, however the CSS and Javascript assets do not load properly.
routes.rb:
authenticate :user do
mount Precious::App, at: 'wiki'
end
So I can access the wiki at /wiki. This works, but gollum tries to load the css files from eg. http://localhost:3000/css/gollum.css which does not work, instead of http://localhost:3000/wiki/css/gollum.css. How do I tell gollum to use the correct prefix?
This is almost certainly a bug in either Rails or Gollum. I know that Gollum does have the ability to map its assets to a subpath, because I do it in my apps (via Rack, though, not Rails routes), like this:
map "/wiki" do
run Precious::App
end
Reporting a bug would probably be your best bet.

generating one single view without ember

I'm working on a app where the backend is rails and the frontend is completely written in ember.js. As soon as you hit the site the index page is already ember.
I now need to make an api in order to share a part of my site through an iframe, and I can't seem to generate only that page without the whole ember application getting in the way. I always get the part that I'm generating for the api, but underneath starts the index page of the ember application.
I don't want the ember app for that part at all.
I've tried to setup a different namespace in my rails route in order to separate both, but that doesn't work correctly. Funny thing is that I also have railsadmin included and that one does work correct, i.e. I don't see the rest of my ember app after every railsadmin page.
My routes.rb
App::Application.routes.draw do
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
root :to => 'application#index'
namespace :api do
resources :share_part, only: [:show]
end
end
you have to use different layouts.. I think you are only using one layout and within this the complete asset pipeline js code incl. the ember js is loaded? check out your html source in the browser and you will see the loaded js files.
simple use an other layout or an "if single page not include all js" for your single view and do not include the whole js. your rails_admin engine has his own layout and his own js code..that´s the reason why it works there.

How do I add asset search paths to Sprockets based on a wildcard subdomain in rails 3.1?

The Rails Asset Pipeline guide instructs you to use config.assets.paths in config/application.rb but I don't have access to the request's subdomain at this point.
I'd like to be able to prepend an extra path (for the current request only) based on the request's subdomain.
My application specific details
It's a basic CMS app. The root domain.com host handles the administrative part with standard controller/view rendering and default asset paths.
Requests to subdomain.domain.com renders the site based on subdomain. It calls prepend_view_path in a before_filter and adds Rails.root.join('vendor/sites/[subdomain]/templates') for the current request only.
I'd like to be able to prepend Rails.root.join('vendor/sites/[subdomain]/assets') to the Sprockets search paths when the request host is [subdomain].domain.com.
EDIT
I ended up just dropping in a mixin for Sprockets::Environment that overwrites the call method:
module SiteAssetsResolver
def call(env)
begin
# prepend path based on subdomain (from env)
super # Sprockets::Server#call
ensure
# remove path based on subdomain
end
end
end
MyApp::Application.assets.extend(SiteAssetsResolver)
Just as you did for your view path, add a before filter and append the new path to Rails.application.config.assets.paths
I got this idea while watching Railscasts #279 Understanding the Asset Pipeline
I agree with commenter on your question that said "The asset pipeline isn't really meant to be compiling your assets each request in production." -- making it not really possible to do exactly what you ask.
So how about an alternative to accomplish what you're really trying to accomplish here, which is different asset resolution for different subdomains. Put your sub-domain specific assets in sub-directories of your asset folders.
Now, in the view/helpers, when you call asset_path or any other helpers that take a relative asset path, ask it for "#{subdomain}/name_of_asset" instead of just "name_of_asset".
Now, because of the way the asset compiler works, it's possible this subdirectory method won't work, you may have to put the subdomain at the beginning of the actual filename instead. "#{subdomain}_name_of_asset". Not sure.
And this still wouldn't give you a sort of 'default fall through' where some assets in some subdomains don't have subdomain-specific assets, they just 'fall through' to the default. Which would be nice. It's possible a way can be figured out to do that too, not sure.
But at any rate, following this approach of asking for a different asset at display-time using logic in view/helper.... is going to get you further than your original suggested approach, which probably isn't possible.

Resources