Ruby on Rails map.root doesn't seem to be working
Have you deleted the static public/index.html
page that Rails creates? If this is still in your app it will be shown instead of the root page you defined in the routes.
Ruby on rails application root
In routes.rb, add:
map.root :controller => 'foo'
Full details in the API.
Simple redirect from public to views in ror
You should have public/index.html deleted and use rails routes instead.
Can I remove the default root in a Rails application without creating a new one?
If you want to render a 404
response, there are two approaches that I can think of.
Firstly, you could route to Rack, and return a simple 404
response:
# config/routes.rb
root to: proc { [404, {}, ["Not found."]] }
Secondly, you could take the obvious route and point root to a controller action that returns 404
:
# config/routes.rb
root to: "application#not_found"
# app/controllers/application_controller.rb
def not_found
render plain: "Not found.", status: 404
end
The third option is, of course, to route to a non-existing action, but I don't think this is a good idea, since the intention is obscured, and could easily be taken for a mistake.
Rails routes - show action as root
Try this in routes.rb
map.connect ':id', :controller => 'articles', :action => 'show'
You'll want to make sure this is a low priority route because of how general it is. I.e., put it toward the bottom of your routes.rb
file but above this section (if you haven't deleted it already):
# low-priorty article show route
map.connect ':id', :controller => 'articles', :action => 'show'
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Problem with link_to_remote in rails
Simply adding a :method => :delete to your link_to_remote call may be the simplest fix for you:
<%= link_to_remote "question-", :update =>"questions-1", :url => {:action => :destroy, :controller => "questions", :id => @question.id}, :method => :delete %>
This should force the call to /questions/:id to use the HTTP DELETE method. If you want the above call to generate the url /questions/destroy/:id instead I believe you would need a manual change to your routes, as the default map.resources doesn't seem to be achieving that result.
Ruby On Rails Routes
As a rule of thumb, you want your resource routes to come before the generic :controller/:action/:id
routes (in fact, I go so far as to delete the generic routes entirely), since routes that are defined first take precedence over the ones that are assigned later.
As for redirecting to /bookings/signmeup if the user is not logged in, you should handle that with a before_filter
:
class BookingsController < ApplicationController
before_filter :check_login
# ...
protected
# This is a GENERIC example; change to fit your authentication method
def check_login
unless user_is_logged_in
redirect_to ...
end
end
end
Nested Resource at Root Level of Path
As noted in this question:
Default segment name in rails resources routing
You should be able to use this plugin:
http://github.com/caring/default_routing
Alternatively, you could specify them manually with something like
map.users '/users', :controller => 'users', :action => 'index',
:conditions => { :method => :get }
map.connect '/users', :controller => 'users', :action => 'create',
:conditions => { :method => :post }
map.user '/:id', :controller => 'users', :action => 'show',
:conditions => { :method => :get }
map.edit_user '/:id/edit', :controller => 'users', :action => 'edit',
:conditions => { :method => :get }
map.new_user '/users/new', :controller => 'users', :action => 'new',
:conditions => { :method => :get }
map.connect '/:id', :controller => 'users', :action => 'update',
:conditions => { :method => :put }
map.connect '/:id', :controller => 'users', :action => 'destroy',
:conditions => { :method => :delete }
map.resources :nodes, :path_prefix => '/:user_id', :name_prefix => 'user_'
# to generate user_nodes_path and user_node_path(@node) routes
That or something like it should give you what you want from map.resources
.
map.resources doesn't work and it seems named routes don't support the ":has_many" or ":belongs_to" options.
Named routes supports has_many, which is used to add another resource level to the URL path. For example
map.resources :users, :has_many => :nodes
generates all the users_path
and user_nodes_path
routes like /users
, /users/:id
, /users/:id/nodes
and /users/:id/nodes/:id
. It's identical to
map.resources :users do |user|
user.resources :nodes
end
Commenting out the "map.resources :users" and uncommmenting the "map.user" line after it seems to work… until you reach a nested resource. Then it spits out the following error:
undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>
That's because map.resources :users, :has_many => :nodes
generates these routes. Only one named route is generated by map.user '/:id', :controller => 'users', :action => 'show'
and that's user_path
.
Rails: redirect all unknown routes to root_url
If your project is powered by rails 3, add simply this line to your routes.rb
match '*path' => redirect('/')
Edit: If you're on Rails 4 or 5
match '*path' => redirect('/'), via: :get
or
get '*path' => redirect('/')
Related Topics
Is It a Good Idea to Purge Old Rails Migration Files
The Program 'Rails' Can Be Found in the Following Packages - Issue with Some App and Rvm
How to Read a Gzip File Line by Line
Grouping Consecutive Numbers in an Array
Printing Elements of Array Using Erb
Why Is Throw and Catch Hardly Ever Used in Ruby
You May Have Encountered a Bug in the Ruby Interpreter or Extension Libraries
Uninitialized Constant > Actioncable::Server::Configuration::Applicationcable
Wicked_Pdf: Footer Height/Styling
How to Create a Charge and a Customer in Stripe (Rails)
Devise Raises Error with Rails 4.2 Upgrade
How to Get a List of Gems That Are Installed That Have Native Extensions
Split Seeds.Rb into Multiple Sections
Sorting an Array by Two Values
Instance Variable, Class Variable and the Difference Between Them in Ruby