Routing to root (Beginner) - ruby-on-rails-3

this is my first time posting to stackoverflow. I am learning Ruby on Rails and i want to ask the reason why my app doesn't redirect to the root i specified in my routes.rb.
I already deleted the public/index.html and also removed it from git.
Jba::Application.routes.draw do
get "home/index"
root :to => 'home#index'
end
Thanks for helping. :)

Make sure you clear the cache on your local browser in order to view the changes you've made to your code.

Related

ActiveAdmin with Rails 4 redirects to localhost in production

I'm using the ActiveAdmin gem in a Rails 4 app, and having trouble when it's deployed in production. It's working fine in development, or when I run it locally in production.
For example, after saving an Asset, instead of redirecting to https://domainname.com/admin/assets/1, it redirects to https://localhost/admin/assets/1, which doesn't exist, so it blows up.
As far as I can tell, I have things set up correctly. Here's my routes file:
Rails.application.routes.draw do
root to: redirect_to('/admin')
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
end
Things seem to work fine in production mode when I'm running it locally, but not when after it's been deployed behind SSL.
Has anyone else had trouble like this when using SSL with ActiveAdmin?
It turns out the nginx configuration was bad. Nothing to do with the Rails code at all!
I got the same localhost redirection problem on prod, but only with destroy method. I implemented a redirection that did the trick :
controller do
def destroy
super do |format|
redirect_to admin_model_path and return
end
end
end
I still don't know what was the root cause.

Prefixing All Rails Routes with a Directory

I have a Rails 3.2 application with numerous resources. How can I change the routing so that every path has an extra directory prepended it? Obvioulsy I know how to do this on a resource by resource basis, but how do I do it to all routes?
So for example:
users
users/1
resources/new
resources/77/edit
assets/66
Would become:
cms/users
cms/users/1
cms/resources/new
cms/resources/77/edit
cms/assets/66
Please check the documentation. Basically, you need:
namespace :cms do
#all your resources
end

devise config.action_mailer.default_url_options not working

I have switched over to pow in order to use ssl in development and I want to switch the host configuration in development, however devise continues to send email prefixed with the localhost:3000 domain. Here is what I have in my config/environments/development.rb file:
config.action_mailer.default_url_options = { :host => 'want_freight.dev' }
I have restarted my server and I have grepped my entire application looking for the offending use of localhost:3000 however my search turned up nothing outside of tmp and log files. Does anyone know why this would not be working??
I changed config.action_mailer.default_url_options to point to Pow's .dev URL, restarted my computer, and, against all odds, it was working again.
So I was able to have some success by adding :only_path => false to the default_url_options hash and using named urls, e.g. user_url( #user.id ) instead of link_to.
This problem was actually related to the devise_async gem causing conflicts with the mailer, I was able to resolve the issue by removing the gem from my gemfile.
No need to restart computer, just restart POW:
touch ~/.pow/restart.txt

routing a different root index

I am new to rails and want to do a simple routing to root operation. My file path is app/views/slots/index.html.erb. When I go in my routes.rb file and see:
You can have the root of your site routed with "root"
just remember to delete public/index.html.
root :to => 'welcome#index'
I change the 'welcome#index' to "slots#index', I get this error from localhost:3000:
No route matches [GET] "/"
In the terminal, I use ctrl c to exit server and $ rails server to restart and still get the error. I watched my instructor do the same exact simple steps yet I get this error. Anyone know what I did wrong?
Edit : my bad, indeed the error is not corresponding to the solution I described below.
It seems you didn't create the slots controller.
You have to create a controller to display files. app/views/slots/index.html.erb is just a view, corresponding to an action of a controller.
So create the file app/controller/slots.rb
class SlotsController < ApplicationController
def index
end
end
You can do this faster with generator. In your terminal, cd to your application path and then
rails generate controller Slots index
To learn Rails I recommend you some reading here : http://guides.rubyonrails.org/getting_started.html

Routing to a Location on the Server

I have several custom routes in my rails 3.0 app, the simplest one of which is.
match "*user", "profile#index", :via => :get
Because of that route physical locations on the server are killed. As an example.
/images/rails.png
tries to route to the images user.
I also have to be able to setup where people access
/<username>/archive.zip
So
/buddy/archive.zip
Where the archive.zip is a physical file on the server that has been generated and put there. How can I achieve this in my routing.
For the later I have an actual folder structure in a root folder for /<username>/archive.zip so I was thinking somem sort of symlink would be easy, but without being able to hit physical locations on the server. I am kind of stuck/confused.
Any help is appreciated.
You probably want to have any static assets get handled by your web server before hitting your rails stack. This is generally done by setting the document root in your webserver to the public/ directory within your rails app to serve your static images/css/js.
This is greatly preferred over allowing Rails to serve static assets because web servers are much faster at handling these sorts of requests, and your not tying up your rails processes for these requests, which are often limited to less than a handful.