Rails keeps saying that the route to my users_controller#destroy does not exist?
Here is the markup:
= button_to 'Delete', user_path(#user), method: :delete, confirm: 'Are you sure?', class: 'small button delete'
Here is an extract from routes.rb:
resources :users, except: [:show]
Here is an extract from users_controllers.rb:
# DELETE /users/1
def destroy
if current_user.id != #user.id
raise('A user may not delete their own account')
end
#user.destroy!
redirect_to users_path, notice: 'User was successfully deleted.'
end
and here an extract from the error message that I keep getting:
Routing Error
No route matches [DELETE] "/users"
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
#davidrac, and #apneadiving both gave the correct answer in the comments above, it is because #user should be user
Related
Each user can create a number of blogs and, when they log in, they are presented with a list of their blogs and a button next to each as below:
= simple_form_for activate_blog_path(blog.id), method: :put do |f|
= hidden_field_tag :active, value: true
= f.button :submit
Even though the path exists in routes, I'm still getting this error message:
No route matches [PUT] "/"
routes.rb:
resources :users
resources :blogs do
member do
get :activate
put :activate
end
end
root 'pages#index'
rails routes:
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
activate_blog GET /blogs/:id/activate(.:format) blogs#activate
PUT /blogs/:id/activate(.:format) blogs#activate
blogs GET /blogs(.:format) blogs#index
POST /blogs(.:format) blogs#create
new_blog GET /blogs/new(.:format) blogs#new
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
blog GET /blogs/:id(.:format) blogs#show
PATCH /blogs/:id(.:format) blogs#update
PUT /blogs/:id(.:format) blogs#update
DELETE /blogs/:id(.:format) blogs#destroy
root GET / pages#index
blogs_controller.rb:
def activate
#blog.active = true
#blog.save
redirect_to root_path
end
What am I doing wrong here?
simple_form_for expects an object (or record) to generate the form tag. To pass custom url and method to a form action you could use
= simple_form_for blog, url: activate_blog_path(blog.id), method: :put do |f|
= hidden_field_tag :active, value: true
= f.button :submit
You need to specify the action and controller as well as the method
= simple_form_for blog, url: url_for(action: :activate, controller: 'blogs'), method: :put do |f|
Ive been following some rails guides and im trying to now implement something on my own for a project im doing and have hit a snag at the first hurdle
I get this error when trying to load the page
ActionController::UrlGenerationError in StepOne#login
Showing /Users/rogan/Sites/authImp/app/views/step_one/login.html.erb where line #3 raised:
No route matches {:action=>"show", :controller=>"step_one"} missing required keys: [:id]
Extracted source (around line #3):
<%= form_for url: step_one_path do %>
form stuff...
then my step_one_controller.rb
class StepOneController < ApplicationController
def new
end
def create
user = User.authenticate(params[:email], params[:password])
if user
pincode = generatePin
puts "one use pin.#{pincode}"
redirect_to "step_two"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
end
this was adapter from a login system i saw in a guide that used SessionsController.rb and form_for url: sessions_path
but my simple changes seem to have broke it, i've looked at my routes as well and they all seem to be in order
edit: heres is my routes
edit edit: changed everything to step_one and to removed the 's' as suggested, I now get
NoMethodError in StepOne#login
undefined method `model_name' for Hash:Class
<%= form_for url: step_one_path do %>
so from one problem to another!
AuthImp::Application.routes.draw do
resources :users
resources :sessions
resource :step_one
get "users/:id" => "users#show"
get "sign_up" => "users#new"
get "log_in" => "step_one#login"
get "step_two" => "sessions#new"
get "log_out" => "sessions#destroy", :as => "log_out"
then my rake routes is as follows
root to: "welcome#index"
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#destroy
step_one POST /step_one(.:format) step_ones#create
new_step_one GET /step_one/new(.:format) step_ones#new
edit_step_one GET /step_one/edit(.:format) step_ones#edit
GET /step_one(.:format) step_ones#show
PATCH /step_one(.:format) step_ones#update
PUT /step_one(.:format) step_ones#update
DELETE /step_one(.:format) step_ones#destroy
GET /users/:id(.:format) users#show
sign_up GET /sign_up(.:format) users#new
log_in GET /log_in(.:format) step_one#login
step_two GET /step_two(.:format) sessions#new
log_out GET /log_out(.:format) sessions#destroy
root GET / welcome#index
your problem is in the naming I think, resources :step_one assumes that you define the StepOnesController (plural), not StepOneController
So, you should either rename your controller or use resource :step_one route (without the s at the end)
It looks like Rails is expecting you to pass in an id to your step_one_path, so the form_for should look like this:
<%= form_for url: step_one_path(#user.id) do %>
Where #user.id would be the id of the user that the form is associated with. Hope that helps. If not, please include your routes.rb file.
<%= form_tag step_one_path do |f| %>
was the form tag necessary, i was going about it wrong
thanks for all the help
I am trying to get the page to be directed to the directory users/new, using a button_to
However everytime I click on it, it generates an error saying
Routing Error
No route matches [GET] "/new_user_path"
Here is my application.html.haml which contains the button_to I am talking about
%html
%head
%title Rotten Potatoes!
= stylesheet_link_tag 'application'
= javascript_include_tag 'application'
= csrf_meta_tags
%body
%h1.title Rotten Potatoes!
= button_to 'Sign Up/Login', 'new_user_path', :method => :get
#main
- if flash[:notice]
#notice.message= flash[:notice]
- elsif flash[:warning]
#warning.message= flash[:warning]
= yield
Here is the result of my rake routes
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PUT /movies/:id(.:format) movies#update
DELETE /movies/:id(.:format) movies#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
This is my users_controller.rb file if that helps
class UsersController < ApplicationController
def new
end
def create
#user=User.create_user!(params[:user])
if !!(#user)
flash[:notice] = "New user #{#user.user_id} was successfully created."
redirect_to movies_path
else
flash[:notice] = "The User Id #{params[:user][:user_id]} already exists"
redirect_to new_user_path
end
end
end
Note that the redirect_to new_user_path (with the conditional statement) works perfectly fine.
Can you tell me where the problem lies? Also, I tried using link_to as well and it still fails.
Should the argument for button_to be the method new_user_path() instead of a string 'new_user_path'?
In rails how do I do a link_to a user show page of the current user. Basically i want a link that go to a account page.
I tried it but i get this.
Routing Error No route matches {:action=>"show", :controller=>"users"}
Routes.rb
LootApp::Application.routes.draw do
get "password_resets/new"
get "sessions/new"
get "static_pages/home"
get "static_pages/help"
resources :sessions
resources :password_resets
resources :email_activations
resources :users do
collection do
get :accept_invitation
end
end
root to: 'static_pages#home'
match "sign_up", to: "users#new"
match '/help', to: 'static_pages#help'
match '/log_in', to: 'sessions#new'
match '/log_out', to: 'sessions#destroy'
Application.html.haml
- if current_user
= link_to "log out", log_out_path
- if current_user.admin?
# your admin
- else
= link_to "My account", user_path
- else
= link_to "log in", log_in_path
Users controller
class UsersController < ApplicationController
before_filter :admin_and_user, only: [:destory, :edit, :show]
def show
#user = User.find(params[:id])
end
rake routes
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
just
<%= link_to current_user.name, current_user%>
The current answer is the best solution, but just to elaborate: The problem that was causing an error in your code was that you needed to specify which user the link should link to. So instead of just user_path should you do like this:
= link_to "My account", user_path(current_user)
A shortcut for this is (as shown in the current answer by #Muntasim)
= link_to "My account", current_user
I'm a rails beginner and have been several hours now trying to figure out why my "generate new password" link, which was supposed to execute my custom update_password action inside my users controller, and then flash the password back on the screen (later i plan to send this by e-mail or sms, but for now this would do) keeps executing the create method/action...
I understand I have two POST methods for the users index screen...but cannot understand (since i even placed it first on the routes file) why create is the one that keeps getting executed. I know it doesn't go to the update method, because i filled it with debugger logging messages, which do not show up anywhere. (and the logging is active since i see the index method logging message)
Here is what I'm doing :
routes.rb file extract :
match 'users?user_id=(:user_id)', to: "users#update_password", via: :post, as: "users_update_password"
resources :users
rake routes (controller users)
users_update_password POST /users?user_id=(:user_id)(.:format) users#update_password
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
signup /signup(.:format) users#new
users_controller.rb
def update_password
logger.info "Inside update_password"
logger.flush
password= generate_password()
logger.debug "Password is #{password}"
#user = User.find_by_id(params[:user_id])
logger.debug "user is #{#user}"
if #user.update_attributes(password:password, password_confirmation:password)
logger.info "inside the if"
flash[:notice] = "New password for user #{#user.name}: #{password}"
logger.debug "Flash is #{flash}"
redirect_to users_path
else
logger.debug "I am on else of update_password"
flash[:alert] = "Name: #{#user.name} password: #{password}"
render 'index'
end
end
def index
logger.info "Inside controller index method"
#users = User.paginate(page: params[:page])
end
_user.html.erb
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete, confirm: "Are you sure?" %>
| <%= link_to "Generate new password", users_update_password_path(:user_id=>user.id), method: :post, confirm: "Are you sure? This will reset current password" %>
<% end %>
Thank you for your help
It might simplify things if you make this a member of your resource routes?
resources :users do
member do
post 'update_password'
end
end
Then path helper probably looks like
link_to "Generate new password", update_password_user_path(user), method: :post
# POST. /users/:id/update_password
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
When you use HTTP POST, the data is not sent in the URL. Your route is looking for data in the URL, which is done via HTTP GET. So if you want to use a POST, make your route something like:
post "users/update_password" => "users#update_password", :as => "users_update_password"