Using path helper in partials generate wrong path in rails 3 - ruby-on-rails

I have my application "http://www.example.com/test".
When I hit the url I get the index page, where I show list of profiles.
To show each profile in the lists, I have used a partial.
Each of the profiles have link to their own show page.
To provide the link addreess I have used Rals path helper like this
profile_path(:id => whatever_id)
The issue is the generated url is like this:-
/profile/whatever_id
It completely skips the app name which is "test". The expected url by me is
/test/profile/whatever_id
Can any one tell what can be the possible cause of this?

It is depends on what you means under "app name". If the app is mounted under /test with Passenger Apache/Nginx module, then Rails app will honor the server-side prefix, and will generate correct URLs automatically, only the development environment will generate urls in the root scope.
If your app is runs standalone or via proxy setup (typical setup if the frontend webserver is an Nginx server), and you want to run it under a prefixed path then you have to use the scope method like this:
Rails.application.routes do
scope(:path => '/test') do
# the rest of your routes go here
end
end
See detailed info in the documentation.

Related

Using multiple Ember apps with the same Rails backend

Hi I have a built out Ember app which works with a single rails backend. I would like to now have several other Ember apps working with rails but i am not sure how to make it work. I am using Oauth, devise and rolify to send logged in users to different ember apps.
Why don't you put all of your ember apps in separate folders inside the Rail's public folder? For example, if you have 2 ember apps, admin and publisher, your folder structure can be something like this:
/app
/public
/admin
/index.html --> ember app
/publisher
/index.html --> ember app
You'll need to define a couple of routes in config/routes.rb, one for admin and one for publisher so that when someone goes to a URL that matches your ember app, the correct file gets served. You can try something like this:
get '/admin/*path' => redirect('/admin/')
get '/publisher/*path' => redirect('/publisher/')
Note that while the above route examples will work, you'll want to do a rewrite instead of a redirect.
Doing it this way will future proof your solution in case you migrate your apps to Ember CLI in the future. With Ember CLI, you can just build your ember apps into the public/admin and public/publisher folders in the future.

How to put a windows installable program on Heroku for user download in a Rails web application?

I have a rails web application that contains a Windows executable file the user can download and install on his PC. This program will communicate with certain peripherals on the user's PC using web services to the web application.
Where should I put such a program in my Rails tree, and how should I make this "available" for users to download?
Assets
Alternatively to putting them in the public dir, you may wish to create a downlods folder in the asset pipeline: /app/assets/downloads
This will give you the ability to call downloads_path, and precompile each file.
In order to get this to work, you'll be best creating a helper method like this:
#app/helpers/application_helper.rb
def downloads_path path
asset_path("/downloads/#{path}")
end
#app/views/application/index.html.erb
<%= downloads_path("download.exe") %>
This has the added benefit of allowing you to precompile the files, and having them accessible from the likes of CDN's, as Michael Szyndel recommended

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.

rails how to generate a http link connect to some path without knowing current domain at backend

For example, I dont know my website name yet, it can be www.abc.com or www.xyz.com
And I want to send a email to my users with the link
www.abc.com/controller1/id/path
or
www.xyz.com/controller1/id/path
How I setup this on rails ???
It doesn't really matter what the hostname of your app is when using the right helper, rails will generate either relative paths or absolute paths for links at runtime (depending on the helper used). url_for is one such helper, link_to is another. Any route that is defined in routes.rb, you get a helper for it. Try looking at the output from rake routes, you'll see which url helpers may already be defined. Just add _path to the routes you see listed in the first column to get a relative path url. For more info on routing see the Rails Guide.

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