routing the page after running scaffold - ruby-on-rails

I am getting below error when I am trying to route the about page which I did with rails g scaffold about.
undefined local variable or method `map' for main:Object Did you mean? tap
This is what I have in the routes.rb file.
map.about '/about', :controller => 'abouts', :action => 'about'

Use
get '/about', :controller => 'abouts', :action => 'about'
or
post '/about', :controller => 'abouts', :action => 'about'
or
match '/about', :controller => 'abouts', :action => 'about', via: [:get, :post]

Run rails g scaffold about this command will generate multiple files, include model, views, and will write resource :abouts to the routes.rb file.
The routes syntax is not right.
map.about '/about', :controller => 'abouts', :action => 'about'
if you just want to create a about page, url set as /about
create a controller named abouts_controller.rb, in the /app/controllers folder
create a action def index; end in abouts_controller.rb
create a view file named abouts.html.erb in the folder app/views/abouts.(you will creat the folder)
write routes to routes.rb
get :about, :controller => :abouts, :action => :index
or
get '/about' to: "abouts#index"
or
match '/about', :controller => 'abouts', :action => 'about'

Related

Reduce complexity of routes

I am attempting to setup my routes.rb so that /sessions/ is not required in the url for logging in and out of the site. Below are my samples to show what I am trying to achieve. Whilst the "second attempt" does in fact do what I want, I'd like to know if there is a more efficient way of doing this. I am very new to rails and I am sure that the routes.rb has some option that can do what I am doing in three large lines.
First attempt
routes.rb
namespace :account do
resources :users
resources :sessions
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_sessions GET /account/sessions(.:format) account/sessions#index
POST /account/sessions(.:format) account/sessions#create
new_account_session GET /account/sessions/new(.:format) account/sessions#new
edit_account_session GET /account/sessions/:id/edit(.:format) account/sessions#edit
account_session GET /account/sessions/:id(.:format) account/sessions#show
PATCH /account/sessions/:id(.:format) account/sessions#update
PUT /account/sessions/:id(.:format) account/sessions#update
DELETE /account/sessions/:id(.:format) account/sessions#destroy
Second attempt
routes.rb
namespace :account do
resources :users
match '/login', :controller => 'sessions', :action => 'new', :via => [:get]
match '/login', :controller => 'sessions', :action => 'create', :via => [:post]
match '/logout', :controller => 'sessions', :action => 'destroy', :via => [:delete]
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_login GET /account/login(.:format) account/sessions#new
POST /account/login(.:format) account/sessions#create
account_logout DELETE /account/logout(.:format) account/sessions#destroy
Can this be done without having to manually specific the match locations? All I want to do is remove /sessions/ as a requirement.
namespace :account do
resources :users #-> account/users
resources :sessions, path: "", path_names: { new: "login", create: "login", destroy: "logout" } #-> accounts/login, accounts/logout
end
I hope you realise you have /login twice in your second example. This simplifies it a bit but you will always have to match each route you want to specify outside any defaults.
namespace :account do
match '/login', to: 'sessions#new', via: [:get]
match '/logout', to: 'sessions#destroy', via: [:delete]
end
In rails3 we should use with_options in following way:
scope '/account' do
match '/login' => "sessions#new", :as => :login
post '/:login' => 'sessions#create', :as => :signup_create
delete '/:logout' => 'sessions#destroy', :as => :logout
end

How do you create a route to a specific show item?

I would like 'about' to route to 'abouts/1'
I tried this:
match 'about' => 'abouts#show/1', :via => get
and it doesn't work. Any ideas?
How about:
match 'about' => 'abouts#show', :via => :get, :defaults => {:id => 1}
What about just removing the 1 from the route and retrieve the record you want directly in the controller method?
# routes.rb
match 'about' => 'abouts#show', :via => get
# abouts_controller.rb
def show
#about = About.find(1)
end

With the route generated by the Vanity gem, what is the Rails route helper I can use?

This is the route the Vanity gem generates:
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+]+/}
end
This is the rake routes:
GET /:vname(.:format) {:vname=>/[A-Za-z0-9\-\+]+/, :controller=>"vanities", :action=>"show"}
How do I use the Rails link helper to link directly to URL mydomain.com/vname?
From the top of my head (sorry, I don't really have the time to test it right now):
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+]+/}, :as => :vanity
end
which you would use like this:
vanity_path(:vname => "marcamillion")

Rails proejct, "Routing Error No route matches "/pages"" Need help

I folllowed this :
http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:user_signup
.
But when I try to access http://localhost:3000/pages/ it returns "Routing Error No route matches "/pages""
This is my routes.rb
Sample4App::Application.routes.draw do
get "users/new"
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/', :to => 'pages#home'
end
This is my home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
I tried everything I can. But still.
Really need help. Thanks
It seems like your missing the actual route for '/pages/'. Try adding this to your routes.rb
match '/pages' => 'pages#home'
Try this:
Sample4App::Application.routes.draw do
get "users/new"
match 'signup' => 'users#new'
match 'contact' => 'pages#contact'
match 'about' => 'pages#about'
match 'help' => 'pages#help'
match 'pages' => 'pages#home'
root :to => 'pages#index'
end
And make sure you have index action in your Pages controller.
Add this in roots
root :to => 'pages#home'
So, you can access http://localhost:3000
or add
match '/pages', :to => 'pages#home'
so you can access http://localhost:3000/pages
try this
root :to => 'pages#index'
like everyone said earlier but did You deleted index.html.erb from /public/ folder? Delete it and try again - this should resolve the problem :)

Rails 2.3.5: how to get routes name

In my applications routes.rb I have defined three routes like the following
map.signup '/signup', :controller => 'users', :action => 'new'
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
Is it possible for me to get the controller and action name for a particular path?
I am looking for some method like this...
def current_routes(a)
end
should return :controller => 'users', :action => 'new' if I call current_routes('signup_path')
Try like this
ActionController::Routing::Routes.recognize_path("/posts/")
If you only have a string with your route (like "signup_path"), then I guess in the context you're using this you should be able to do
ActionController::Routing::Routes.recognize_path(send("signup_path".to_sym))

Resources