I have write down this in routes file
resources :users
match '/signup', to: 'users#new', via: [:POST, :get, :post]
match '/edit_user', to: 'users#edit', via: 'get'
and this is my function in users controller
def edit
#user = User.find(params[:id])
end
I am getting error Please help me..
No route matches [GET] "/edit_user/1"
match '/edit_user', to: 'users#edit', via: 'get'
You are missing the /:id on the route. Fixed:
match '/edit_user/:id', to: 'users#edit', via: 'get'
Related
routes.rb file is
Rails.application.routes.draw do
root 'pages#home'
match '/contact', to: 'pages#contact', via: 'get'
match '/home', to: 'contact#pages', via: 'get'
pages_controller is
class PagesController < ApplicationController
def home
end
def contact
end
end
I get routing error saying "uninitialized constant ContactController". Does anyone know how to fix this?
Do Change your routes.rb file
Rails.application.routes.draw do
root 'pages#home'
match '/contact', to: 'pages#contact', via: :get
match '/home', to: 'pages#home', via: :get
end
Would work!!!
In Rails 4 with match methods must specify HTTP method otherwise would get RuntimeError "You should not use the match method in your router without specifying an HTTP method. (RuntimeError)"
match is deprecated. Try
root 'pages#home'
get '/contact', to: 'pages#contact'
get '/home', to: 'pages#home'
Im using RoR to write my website and i've run into this issue with routes.
My routes file looks as such
root to: "static#index"
match '/index', to: 'static#index', via: [:get, :post]
match '/download', to: 'static#download', via: [:get, :post]
match '/terms', to: 'static#terms', via: 'get'
match '/privacy', to: 'static#privacy', via: 'get'
match '/jobs', to: 'static#jobs', via: 'get'
match '/android', to: 'static#android', via: [:get, :post]
match '/apply', to: 'static#apply', via: 'get'
match '/faq', to: 'static#faq', via: 'get'
When I preform a GET request to https://www.website.com/ the page gets displayed.
However when I preform a POST request to the same address it returns a 404 page not found.
I do not have a index.html in my public directory.
So why is this POST request returning a 404 when it is matched to both GET and POST and the GET works correctly?
EDIT
i've added this line
match '/', to: 'static#index', via: [:get, :post]
just below my root to declaration, however now my page shows a 422 error instead of a 404
You can try this:
post '/' => "static#index", as: "root"
Rails routes: resourcing from the root path "/"
Your root URL is the one that handles https://www.website.com/ from your example. When you specify
root to: "static#index"
You have declared a GET route for /, which maps to StaticController#index. The POST request that you are making to https://www.website.com/ is not handled by the root route, thus properly returns a 404.
I'm guessing that you want the POST request to go to either /index, /download or /android. You need to specify the endpoint fully in the URL in order for the request to be routed properly.
My preference and I think the cleanest implementation would be:
resources :static, :only => [:index, :create], :path => "/"
I wish to map new_user path of the user resource to 'ROOT_DOMAIN/new' URL. My routes.rb looks something like this:
#new_user GET /users/new(.:format) users#new
# User Resource
match 'new', :to => 'users#new', via: [:get, :post]
resources :users do
collection do
match 'new', :to => 'users#new', via: [:get, :post]
end
end
So while I'm able to hit the /new URL to the desired action with
match 'new', :to => 'users#new', via: [:get, :post]
but the new_user path still leads to '/users/new'. How to remove the controller_name from the new_user method?
Your new_user_path takes you to user#new because that's how rails build resourceful routes. Have a look at rails guides to learn more. You need to pass the as: option to your routes in order to give them proper helper methods like:
post '/home' => "home#index", as: :home
This will give you two helpers home_path and home_url
Now you have
resources :users do
collection do
match 'new', :to => 'users#new', via: [:get, :post]
end
end
If you do rake routes in your terminal you'll see that new_user helpers are assigned to user#new. You need to give it a different path helper and then assign your new_user helpers to your custom route
resources :users do
collection do
match 'new', :to => 'users#new', via: [:get, :post], as: :old_new_user
end
end
and then you can use your new_user helper for your custom route
match 'new', :to => 'users#new', via: [:get, :post], as: :new_user
You can check your path helper by doing rake routes in terminal
My app runs perfectly on my localhost, but when I push it to Heroku, my home page gives me a message that says 'The page you were looking for doesn't exist.' Other pages (not all, but most) within the app display correctly on Heroku. Here is the output from running heroku run cat config/routes.rb:
Running `cat config/routes.rb` attached to terminal... up, run.8019
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :teams
resources :players
resources :matchups
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/updatepts', to: 'static_pages#updatepts', via: 'get'
match '/findMatchup', to: 'static_pages#findMatchup', via: 'get'
The other page that notably does not display is matchups/:id. Any ideas as to why this is displaying with no problem locally, but will not work on Heroku? Thanks!
Edit: I am running Rails 4 w/ Ruby 2.
My underlying problem was that a partial that was being rendered on my home page (and the one other page not displaying) was calling a field that was nil because the data in my heroku database was not as extensive as the data in my local database.
I'm stuck in a Rails tutorial trying to configure Rails routes.
This is my routes.rb:
SampleApp::Application.routes.draw do
match '/', :to => 'static_pages#home', :via => :get
match '/help',:to => 'static_pages#help', :via => :get
match '/about', :to => 'static_pages#about', :via => :get
match '/contact', :to => 'static_pages#contact', :via => :get
When I try to access "localhost" I get:
No route matches [GET] "/static_pages/help" Try running rake routes
for more information on available routes.
I also tried:
match '/', :to => 'static_pages#home'
match '/help',:to => 'static_pages#help'
match '/about', :to => 'static_pages#about'
match '/contact', :to => 'static_pages#contact'
but that gives the same error. It only works when I use:
get 'static_pages/about'
How can I get localhost to work?
Try putting the following at the end of routes.rb
match ':action' => 'static#:action'
A request to /help will then render app/views/static/help.html.erb. Don't forget to create the static controller though, even if it's empty.
Try this:
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'