Edit Route No Matches Routing Error GET - ruby-on-rails

Hey guys I am using Rails 5. I am trying to let my users edit there info, password etc but when I go to this route.
http://0.0.0.0:3000/users/edit/1
The error I get is
No route matches [GET] "/users/edit/1"
I do have a user with an ID of 1 and I have checked in my rails console to verify.
I have an edit template, and the edit controller and the edit method in the controller but its just not working. What am I doing wrong? All help is welcome, thank you!
ROUTES
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'pages#home'
get 'pages/tierlist', to: 'pages#tierlist'
resources :articles
get 'signup', to: 'users#new'
resource :users, except:[:new]
end
USERSCONTROLLER
class UsersController <ApplicationController
def new
#user = User.new
end
def create
#user = User.create(user_params)
if #user.save
flash[:success] = "Welcome to the OP-OR-Nah Community #{#user.username}"
redirect_to articles_path
else
render 'new'
end
end
def edit
#user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(:username, :email, :password)
end
end
edit.html.erb
<h1>Edit your info</h1>
Also when I rake routes this is what I get back
rake routes |grep edit
edit_article GET /articles/:id/edit(.:format) articles#edit
edit_users GET /users/edit(.:format) users#edit

You have a typo in your file that is breaking the route.
Should be
resources :users, except: [:new]
That route typically should look like
"/users/:id/edit"
when you run rake:routes.

Default url for edit is pluralize_model_name/id/edit so users/1/edit. Anyway, you wrote resource and not resources so I think you have not that route. resource is a different method that generate something like
GET /user/new
GET /user
GET /user/edit
PATCH/PUT /user
DELETE /user
POST /user

Related

Second root which redirects to users profile page when logged in without using Devise

The problem I'm currently having is setting up the root page to redirect to the logged in users profile page. My root page is a simple landing page with links to login or sign up. I don't want that page to be accessible once logged in because its fairly pointless to go there.
Here is my GitHub repo for this project if you need more context or if I didn't provide something you would need.
What I was going to try to do is add a before_action to the SessionsController
before_action :user_authenticated, only: [:new]
def user_authenticated
#user = User.find_by(email: params[:session][:email].downcase)
if logged_in?
redirect_to #user
end
end
And a second root above the first one in the routes.rb file.
root to: 'sessions#new'
I can't exactly figure out what's wrong with that line and if I remove it, I just get another error. I'm assuming something in that line doesn't have a value or it doesn't know about something. I feel like this should be a trivial problem, but I keep getting hung up on it.
Here is how my sessions_controller.rb & routes.rb files are currently without those snippets of code
# sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
#user = User.find_by(email: params[:session][:email].downcase)
if #user && #user.authenticate(params[:session][:password])
log_in #user
params[:session][:remember_me] == '1' ? remember(#user) : forget(#user)
redirect_to #user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
# routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
It seems like you can use constraints also, but I couldn't get it to work. There seems to be tons of answers when using Devise, but I feel it would be kinda odd to add in Devise just to redirect back to their profile.
This is a stackoverflow answer I looked at that could be relevant, but didn't have much luck with it.
Specify a different root path for logged in users without using Devise
In your static_pages_controller.rb file just add:
def home
if logged_in?
redirect_to current_user
end
end

Unfamiliar error: ActionController::RoutingError at /show uninitialized constant UserController?

I am getting this error:
ActionController::RoutingError at /show
uninitialized constant UserController
I have checked my routes, and controller several times and they seem fine so I will post them below
class UsersController < ApplicationController
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:image, :name)
end
end
the route:
get 'index' => 'users#index'
get 'show' => 'user#show'
the attempted link to the show page from the index view:
<h4 class="media-heading"><%= link_to user.name, show_path %></h4>
Thanks for the help, will gladly post more info if needed.
You have an error in routes:
get 'show' => 'user#show' should be get 'show', to: 'users#show'
You don't have show action in your controller
I would use RESTful routes, which is simply:
resources :users # this will generate routes for you
You can specify which actions you want, or which you want to restrict using only or except options as #D-Side says in comments

Rake routes 'match' mapping to the wrong action

My question has to do with mapping to controllers/actions using named routes. I am trying to map '/profile' to 'customers#show'. My routes file looks like this:
root :to => 'pages#home'
## named routes
match "profile" => "customers#show", :as => 'profile'
match 'signin' => 'sessions#new', :as => 'signin'
match 'signout' => 'sessions#destroy', :as => 'signout'
resources :customers do
member do
get 'add_card'
post 'submit_card'
end
end
resources :payments, :only => [:show, :new]
delete 'payments/delete_recurring_payment'
post 'payments/submit_non_recurring'
post 'payments/submit_recurring'
resources :sessions, :only => [:create, :destroy, :new]
Running 'rake routes' gives me this:
root / pages#home
profile /profile(.:format) customers#show
signin /signin(.:format) sessions#new
signout /signout(.:format) sessions#destroy
add_card_customer GET /customers/:id/add_card(.:format) customers#add_card
submit_card_customer POST /customers/:id/submit_card(.:format) customers#submit_card
customers GET /customers(.:format) customers#index
POST /customers(.:format) customers#create
new_customer GET /customers/new(.:format) customers#new
edit_customer GET /customers/:id/edit(.:format) customers#edit
customer GET /customers/:id(.:format) customers#show
PUT /customers/:id(.:format) customers#update
DELETE /customers/:id(.:format) customers#destroy
new_payment GET /payments/new(.:format) payments#new
payment GET /payments/:id(.:format) payments#show
Here is where I'm stumped. When I go to localhost:3000/profile I get a routing error saying this:
No route matches {:action=>"edit", :controller=>"customers"}
This seems odd because there is indeed a route to 'customers#edit' due to my declaring customers as a resource.
However, when I go to 'localhost:3000/signin' I get routed to 'customers#show' which is where I want '/profile' to route to.
It seems like my routes are 'one off' in my routes file but I have no idea why. Any help would be much appreciated.
Thanks
UPDATE 1: Adding my Customers Controller
class CustomersController < ApplicationController
layout "payments_layout"
def show
#customer = current_user
get_account_info(#customer)
get_payment_history(#customer, 10)
end
def new
#title = 'Create an account'
#customer = Customer.new
end
def edit
#customer = current_user
get_account_info(#customer)
end
def update
#customer = current_user
if #customer.update(params[:customer])
redirect_to #customer
else
#card_message = "Use this form to add a credit card to your account. You must have a credit card associated with your account in
in order to make payments on our system."
get_account_info(#customer)
render 'edit'
end
end
def create
#customer = Customer.new(params[:customer])
if #customer.save_and_get_stripe_id
sign_in(#customer)
redirect_to #customer
else
#title = 'Create an account'
render 'new'
end
end
def add_card
#customer = current_user
get_account_info(#customer)
#card_message = "Use this form to add a credit card to your account. You must have a credit card associated with your account in
in order to make payments on our system."
end
def submit_card
#customer = current_user
res = #customer.add_or_update_card(params)
if res
redirect_to #customer
else
#error = res
customer.get_account_info(#customer)
render 'add_card'
end
end
end
Check your views.
Do you have a link to edit your user profile?
It seems like you actually route to the right controller and action but have some links that rake can't route, e.g. you don't pass the ID to your edit_customer_path link.

Routing Error Try running rake routes

I got 2 diferents message but whit the same error, Im using a devise for users... Here is my routes.rb
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users
whit my routes.rb just like that i got the next error
uninitialized constant UsersController
Try running rake routes for more information on available routes.
but if a remove the "resourses: users" i got the following error
No route matches [GET] "/users/27"
I put my UsersController
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to user_session_path
else
redirect_to new_user_session_path
end
end
def show
#user = User.find(params[:id])
#redirect_to #user
end
end
The filename for your users model must be users_controller.rb and be in the controllers folder.

No route matches {:action=>“show”, :controller=>“users”}

i following happens, this is my Usercontroller
class UserController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to user_session_path
else
redirect_to new_user_session_path
end
end
def show
#user = User.find(params[:id])
#redirect_to #user
end
end
in my routes.rb I have the following:
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :user
resources :user do
#resources :car
end
When I run it on my browser I get this:
Routing Error
No route matches {:action=>"show", :controller=>"user"}
Try running rake routes for more information on available routes.
I don't know why this happens???
You need to pass the User object or at least the user id to the link_to method where you're creating the link that you're clicking. Try something like <%= link_to user.name, user %>
Also make sure that your controller is properly named e.g. UsersController (plural).
Rename both the file:
'user_controller.rb' => 'users_controller.rb'
and the constant at the top:
UserController => UsersController
File names must match constants, and the convention is for models to be singular, and controllers to be plural (matching the table).
and try visiting
/users/1

Resources