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

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'?

Related

display user's profile fail

Please read at the very bottom, I edited my post, I still need help.
regards
I am using devise to authenticate the users and admin with (admin: true). As an admin I want to visit the users profile's pages but I always arrive on my own profile ( as the current_user). I don't know how to do...
Users could see others users profile too
Thanks for your help
users/index.html.slim
.container
h1 All the users
.row
table.board
thead
tr
th First Name
th Last Name
th Email Address
th Action on User
hr
tbody.board
-#users.each do |user|
.row
.col-xs-3
= user.first_name
.col-xs-3
= user.last_name
.col-xs-3
= user.email
.col-xs-1
#The problem is this link
= link_to 'View', user_path(user.id), class:'btn btn-success'
.col-xs-1
= link_to 'Remove', user_path(user), class:'btn btn-danger', method: :delete, data: {confirm: "Are you sure?"}
hr
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def show
#binding.pry
##user = User.find(current_user)
##user.id = User.find(params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos= Tuto.all
end
def index
if current_user.admin == true
#users = User.all
else
redirect_to root_path
end
end
def destroy
#user = User.find(params[:id])
#user.destroy
flash[:success] = "User was successfully deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :id)
end
end
The routes
#edited
Rails.application.routes.draw do
namespace :users do
resources :tutos
end
resources :tutos, only: [:show]
resources :tutos do
member do
put "like", to: "tutos#upvote"
end
end
get "/register", to: "devise/registrations#new", as: :register
get "/login", to: "devise/sessions#new", as: :login
get "/logout", to: "devise/sessions#destroy", as: :logout
get "/account", to: "users#show", as: :account
get "/login" , to: "devise/sessions#new", as: :new_user_session
post "/login" , to: "devise/sessions#create", as: :user_session
delete "/logout" , to: "devise/sessions#destroy", as: :destroy_user_session
devise_for :users, skip: [:sessions]
resources :users
root "home#landing"
end
edit rake routes gives :
$ rake routes
Prefix Verb URI Pattern Controller#Action
users_tutos GET /users/tutos(.:format) users/tutos#index
POST /users/tutos(.:format) users/tutos#create
new_users_tuto GET /users/tutos/new(.:format) users/tutos#new
edit_users_tuto GET /users/tutos/:id/edit(.:format) users/tutos#edit
users_tuto GET /users/tutos/:id(.:format) users/tutos#show
PATCH /users/tutos/:id(.:format) users/tutos#update
PUT /users/tutos/:id(.:format) users/tutos#update
DELETE /users/tutos/:id(.:format) users/tutos#destroy
like_tuto PUT /tutos/:id/like(.:format) tutos#upvote
tutos GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
new_tuto GET /tutos/new(.:format) tutos#new
edit_tuto GET /tutos/:id/edit(.:format) tutos#edit
tuto GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
account GET /account(.:format) users#show
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
root GET / home#landing
edit
after the last edits I still have a problem....
when I try to go on the account_path I have this error
Last edit
Just to remind you, I am using devise:
As a user logged in, if I want to see my own profile, I use
account_path (and this work well)
The link for visiting a user's profile page looks like this:
= link_to 'View', user_path(user)
but it looks like it point exactly like: account_path. ( So on my profile, not on the user I want to visit)
I am not sure what to use in my controller, if I use #user = User.find(user_params[:id]) || current_user or ##user = User.find(user_params[:id])
I have the following error:
ActionController::ParameterMissing in UsersController#show
param is missing or the value is empty: user
If I use #user = User.find(current_user).
I am redirected on my own profile each time....
def show
#binding.pry
##user = User.find(current_user)
##user = User.find(user_params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos = Tuto.all
end
This will set #user to current user or to requested user for admins
#user = current_user.admin? ? User.find(params[:id]) : current_user
In #show you could something like:
# Assuming params[:id] is the ID of the user's profile you're trying to view
def show
user_id = current_user.admin? ? params[:id] : current_user.id
#user = User.find(user_id)
end
The problem is in your route file remove 'as user do' blocks it makes some bizzare things :
user GET /users/:id(.:format) users#show
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
Do you want to use the account_path to view another user's profile, or could you go to /users/2 for the user with id of 2? That uses the show action in the controller and routes to the user path using the relevant id?

Rails Simple Form - No route matches

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|

NoMethodError new to Rails

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

Rails keep saying delete route does not exist

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

Adding password reset functionality to Rails app after Hartl tutorial

I'm new to RoR and have been working my way through the Hartl tutorial (which has been great). I've followed up through Chapter 9 successfully (tweaking things a bit since my ultimate goal is not to make a microposts site). At that point, I decided that I would like to add a 'remember me' check box and reset password functionality to my app, so I bounced over to the railscast tutorial (as suggested by Hartl). The check box went very smoothly, but I've hit a brick wall with the password reset section. It's been one error after the next. I have to admit that I couldn't help myself and tweak a little - I tried to use theform_for syntax instead of the form_tag syntax. I've gotten as far as being able to submit an email address, but then I get a No route matches [POST] "/reset_password/new" message. I've spent the last two days reading similar posts on stackoverflow and trying out the suggestions, but I just can't seem to come up with something that works. Please help!
Here's the nitty gritty:
My password reset view is located at /app/views/reset_password/new.html.erb:
<% provide(:title, 'Reset Password') %>
<h1>Reset Password</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for #user, url: new_reset_password_path do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.submit "Reset Password", class: "btn btn-large btn-methyl" %>
<% end %>
</div>
</div>
My controller is located at /app/controllers/reset_password_controller.rb:
class ResetPasswordController < ApplicationController
def new
#user = User.new
end
def show
end
def create
#user = User.find_by_email(params[:email].downcase)
user.send_password_reset if user
redirect_to root_path, notice: "Email sent with password reset instructions."
end
def edit
#user = User.find_by_password_reset_token!(params[:id])
end
def update
#user = User.find_by_password_reset_token!(params[:id])
if #user.reset_password_sent_at < 2.hours.ago
redirect_to_new password_reset_path, alert: "Reset password request has expired."
elsif #user.update_attributes(params[:user])
redirect_to root_path, notice: "Password has been reset!"
else
render :edit
end
end
end
My routes are located at /config/routes.rb:
Methylme::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :reset_password
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
.
.
.
end
Finally, $ rake routes reports the following:
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
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
reset_password_index GET /reset_password(.:format) reset_password#index
POST /reset_password(.:format) reset_password#create
new_reset_password GET /reset_password/new(.:format) reset_password#new
edit_reset_password GET /reset_password/:id/edit(.:format) reset_password#edit
reset_password GET /reset_password/:id(.:format) reset_password#show
PUT /reset_password/:id(.:format) reset_password#update
DELETE /reset_password/:id(.:format) reset_password#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
Thanks in advance for your help!
I don't think you want to link to new_reset_password_path (new) in your password reset view, but to reset_password_path (create), which does send the reset password email.
If your routes don't do what you expect (for instance, the create route does not have an associated xxx_path name) you should simply declare them individually, with
post '/reset_password', to: 'reset_password#create', as: 'send_reset_password' # for example
...
This is one of the best authentication tutorial by Ryan,
http://railscasts.com/episodes/250-authentication-from-scratch-revised

Resources