No route matches [GET] "/" - ruby-on-rails

I'm new to rails so it may sound quite naive.I'm getting this error
No route matches [GET] "/"
Here is my routes.rb
MyApp::Application.routes.draw do
match 'welcome/contact' => 'welcome#index'
end
Here is my controller
class WelcomeController < ApplicationController
def index
redirect_to :action => :contact
end
def contact
end
end
And i have a contact.html.erb in my app/view/welcome/.What am i doing wrong?

I don't understand what you want to do. But I think you want your view Welcome/contact as your index page, if this is correct, you only have to change your routes.rb file like this:
root to: 'welcome#contact'
and you have to remove the index.html file from the public folder.
On the other hand, you can read more of rails routes here

You need to create a route for the actions other than CRUD actions in controller.This will solve the issue for all actions.
match ':controller(/:action)'

What you want to do is to RENDER the contact page, not to redirect to another controller and action.
Just put the code in the contact view into the app/views/welcome/index.html.erb file, and live happily.

You need to add a contact action to your WelcomeController
class WelcomeController < ApplicationController
def index
redirect_to :action => :contact
end
def contact
end
end

Related

Routing error for rails - uninitialized constant SubscribersController

I have a Subscriber model that takes in a "phone_number" and a "visit" integer. I have two controllers Subscribers and Visits(super and sub) I have never worked with nested controllers before and I'm having some issues with namespace I believe. Because I getting back the uninitialized constant error. Basically the subscriber controller signs up a subscriber and the visit controller counts the amount of times they've visited by user input of their phone_number. Why am I getting this error? I'll show my code for clarity.
CONTROLLERS
class Subscribers::VisitsController < ApplicationController
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if #subscriber
#subscriber.visit += 1
#subscriber.save
redirect_to subscribers_visits_new_path(:subscriber)
else
render "new"
end
end
end
class SubscribersController < ApplicationController
def index
#subscriber = Subscriber.all
end
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.create(subscriber_params)
if #subscriber.save
flash[:success] = "Subscriber Has Been successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end
ROUTES
Rails.application.routes.draw do
devise_for :users
resources :subscribers, except: :show
get '/subscribers/visits/new', to: 'subscribers/visits#new'
root "welcomes#index"
VIEWS
<h1>hey</hey>
<%= form_for #subscriber do |form| %>
<div class="form-group">
<p>
<%= form.label :phone_number %>
<%= form.text_field :phone_number %>
</p>
<% end %>
ERROR
Hmm, my guess is you are trying to route url subscriber/visits/new to new action in VisitsController?How about changing this line:
get '/subscribers/visits/new', to: 'subscribers/visits#new'
to:
namespace :subscribers do
get '/visits/new', to: 'visits#new'
end
Also try to move this block above resources :subscribers, except: :show if you still get the error.
Cheers
You probably do not need to inherit one controller from another. Simply define the controllers as you normally would:
app/controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
# methods for Subscribers
end
in app/controllers/visits_controller.rb
class VisitsController < ApplicationController
# methods for Visits
end
Note that these must to be located in separate files, so that Rails can find the correct source file by the name of the object that it's looking for. This is a Rails naming convention.
Regarding your routes, you'll need to change to use one of 4 route formats. Reading the section on Adding More RESTful Actions in the Rails Routing from the Outside In guide might help.
1) To route visits as a nested resource, which is what it appears you're actually trying to do, you would use this:
resources :subscribers, except: :show do
resources :visits
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
GET /subscribers/:id/visits
GET /subscribers/:id/visits/:id
GET /subscribers/:id/visits/:id/edit
PATCH /subscribers/:id/visits/:id/update
DELETE /subscribers/:id/visits/:id/destroy
This is the typical route structure for nested resources and separate controllers.
2) To make visits#new a simple collection (non-member) action in the VisitsController, then you likely want this:
resources :subscribers, except: :show do
collection do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/visits/new
POST /subscribers/visits
This is typically used to add new top-level routes in an existing resource and controller.
3) To construct visits as member actions, use this:
resources :subscribers, except: :show do
member do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
This is normally used to add new member routes in an existing resource and controller.
4) To simply make visits routes appear to be included in subscribers, you could use this:
get '/subscribers/visits/new', to: 'visits#new'
post '/subscribers/visits', to: 'visits#create'
resources :subscribers, except: :show
This will produce these routes:
GET /subscribers/visits/new
POST /subscribers/visits
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
This may be used to make arbitrary routes appear to be included in an existing resource, when they really may be independent.

static pages in rails app missing template

I have to add a static page to my rails app.
Here's the logic I am following.
I made a directory called pages and put a file called signup.html.erb there.
The path in my another html file is pages/signup
In my routes.rb, I have get 'pages/signup'
I made a pages controller and there I have something like below
class PagesController < ApplicationController
def signup
end
end
I get below error.
Missing template pages/signup, application/signup with
What's wrong here?
just write in routs.rb
get '/pages/signup', to: 'pages#signup'
and in pages_controller.rb write
def signup
#page = Page.new
end
def create
# write create function
end
In my routes.rb, try
get 'pages/signup'
match "signup", :to => "pages#signup"
on rails 4
get 'pages/signup'
match "signup", :to => "pages#signup", via: :all
Because your controller isn't in the spree namespace change the full path of the html file from
app/views/spree/pages/signup.html.erb
to
app/views/pages/signup.html.erb

Issue with routes not calling the correct page

So I always thought routes was straightforward but it clearly isn't. I want my page to show data.html.erb but it keeps on showing show.html.erb. These are my only two views in the user folder
My controller is:
class UserController < ApplicationController
def data
render :json => User.including_relationships
end
end
and my routes.rb is:
Rails.application.routes.draw do
get 'users/:data' => 'users#data'
resources :user
end
I always seem to get the show.html.erb page instead of the data.html.erb. Im sure there's something easy to fix here but what?
The : before data in your route denotes a variable, try
Rails.application.routes.draw do
get 'users/data' => 'users#data'
resources :user
end

Index being redirected to Show View

When I go to /relationships/index it displays the show page even if it is not mentioned in the controller? Then when I try to just go to an index view without a show page created I get the following error: Unknown action: The action 'show' could not be found for relationships controller, even with no mention of it in the controller or a view file for the action.
routes.rb
Mymanual::Application.routes.draw do
resources :validation_rules
resources :validations
resources :product_types
resources :products
resources :connections
resources :relationships
root :to => 'products#index'
end
relationships_controller.rb
class RelationshipsController < ApplicationController
def index
end
def new
#relationship = Relationship.new
end
end
Then just HTML in a index.html.erb, show.html.erb and new.html.erb file.
It's because /relationship path is for relationships#index. If you go to /relationships/index, Rails router assumes you want to go to relationships#show path with params[:id] == 'index'.
Even though you want the index action, you don't enter index in the url. Otherwise, it thinks you're trying to find a Relationship with an id of index.
So just go to /relationships instead.
Go to a console and then run:
rake routes
It will give you all routes your app understand
In some place of this routes you will have something like:
relationships relationships#index /relationships
relationship relationships#show /relationships/:id
The issue that you have is that the app understand /relationships/index as relationships with id=index
The fix? use /relationships url

Get routes from database in Rails

How to load routes from database?
I have table Post with column :url. There is part of the url in this column, e.g.:
about
progs/us
progs/us/info
empty
etc.
How to set routes for this? Result example:
http://mysite.com/progs/us for the page http://mysite/posts/2
You can intercept any url by this route:
# routes.rb
get '*url' => 'posts#show', format: false
Note: this route should be declared last in your config
And controller looks like following:
# posts_controller.rb
class PostsController < ApplicationController
def show
#post = Post.find_by_url!(params[:url])
# by default "show" view is rendered with "post" variable
end
end
Here's how I made routes like this
www.example.com/jenny250
www.example.com/bob1985
At end of routes.rb
get ':username' => 'users#show'
In the users controller
def show
#user = User.find_by_username(params[:username])
# ...
end

Resources