Im getting this error after I submit the form.
undefined method `user_url' for #
I am not using the resources :users in my route file and i guess thats why i get the error.
My routes.rb
TaskManager::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
get 'users/new' => 'users#new'
post 'users/' => 'users#create'
post 'users/:first_name' => 'users#show'
end
and my users_controller.rb
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
#user.save
if #user.save
redirect_to #user
else
end
end
def show
#user = User.find(params[:first_name])
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email)
end
end
From your code, I'm guessing that the route you want is the post 'users/' => 'users#create'. To Achieve that, I'll you should need to do is change that line to post 'users/' => 'users#create', :as => 'user'.
For future reference: http://guides.rubyonrails.org/routing.html
Related
I am trying to write an update function, which allows to pick a user in a list of users and update that user to make it an admin. The important function in the controller should be def change_admin.
Thanks for your help!
I tried several options, but I run into that error:
Couldn't find User with 'id'=
My Controller:
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def new
#user = User.new
end
def index
#users = User.where(activated: true).paginate(page: params[:page])
end
def show
#user = User.find(params[:id])
redirect_to root_url and return unless #user.activated
end
def edit
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "Der Nutzer wurde gelöscht"
redirect_to users_url
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
redirect_to root_path
else
render 'edit'
end
end
def create
#user = User.new(user_params)
if #user.save
#user.send_activation_email
flash[:info] = "Bitte öffnen Sie Ihr E-Mail Postfach, um den Account zu aktivieren."
redirect_to root_url
else
render 'new'
end
end
def admin
#users = User.where(activated: true).paginate(page: params[:page])
end
def change_admin
#user = User.find(params[:id])
#user.update_attribute(:admin,true)
respond_to do |format|
format.html { redirect_to admin_path }
end
end
# Before filters
# Confirms the correct user.
def correct_user
#user = User.find(params[:id])
redirect_to(root_url) unless current_user?(#user)
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation,
:mat_number, :ects, :grade_avg, :enrolled, :matched_institute)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
I also tried to delete the user.find line, but that gives me another error:
undefined method `update_attribute' for nil:NilClass
My routes file:
Rails.application.routes.draw do
resources :preferences
resources :institutes
get 'password_resets/new'
get 'password_resets/edit'
get '/users_show', to: 'users#show'
get '/users/new'
root 'static_pages#home'
get '/home', to: 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/matching', to: 'static_pages#matching'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/admin', to: 'users#admin'
post '/change_admin', :to => 'users#change_admin', as: 'change_admin'
get '/performance_show', to: 'users#performance_show'
get '/performance_update', to: 'users#performance_update'
post 'preferences/create_all', :to => 'preferences#create_all'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
post 'preferences/delete_matching', :to => 'preferences#delete_matching'
post 'preferences/read_and_show_ofv', :to => 'preferences#read_and_show_ofv'
post 'preferences/read_matching', :to => 'preferences#read_matching'
post 'preferences/optimize_matching', :to => 'preferences#optimize_matching'
post 'preferences/optimize', to: 'preferences#optimize'
end
Your routes are in pretty bad shape. You have tons of duplication as well as routes that are missing the :id segment to make them work:
get '/users_show', to: 'users#show'
get '/users/new'
post '/change_admin', :to => 'users#change_admin', as: 'change_admin'
resources :users already declares a proper show route as GET /users/:id and new as /users/new.
To add additional RESTful routes you should instead pass a block to resources:
resources :users do
# this should be PATCH not POST
patch :change_admin
end
This will create the proper route as /users/:id/change_admin.
You are also using the wrong HTTP verb in many places like for example get '/performance_update', to: 'users#performance_update'. Never use GET for actions that create or alter a resource as the call ends up in the browsers history and may be cached.
Adding update, change, create in the path of your route should be a big red flag that you´re doing it wrong.
I would recommend that you read the guides thoroughly before adding more cruft.
I have a page at
http://localhost:3000/email/correspond?id=6
It is a form to send an email. When sent it changes to:
http://localhost:3000/email/correspond
And throws up the error; Couldn't find User with 'id'= Which is caused by
def set_user
#user = User.find(params[:id])
end
which is in email_controller (see below)
I can't understand the problem because the id is given in the sending url.
Can anybody explain?
from email_controller.rb
`
def correspond
#cuser = #current_user
#recipient = #user
#title = "Email"
if param_posted?(:message)
#message = Message.new(params[:message])
if #message.valid?
PostMailer.say_hello(#cuser, #recipient, #message).deliver_now
flash[:notice] = "Email sent."
redirect_to user_path(#recipient)
end
end
end
def set_user
#user = User.find(params[:id])
end`
routes.rb
Eskvalleytales::Application.routes.draw do
root :to => 'users#index'
get 'user/:id' => 'user#show'
get 'user/search' => 'user#search'
resource :sessions
get 'session/destroy' => 'sessions#destroy'
get 'friendship/accept' => 'friendship#accept'
get 'friendship/decline' => 'friendship#decline'
get 'friendship/cancel' => 'friendship#cancel'
get 'friendship/delete' => 'friendship#delete'
get 'friendship/create' => 'friendship#create'
get 'email/correspond' => 'email#correspond'
post 'email/correspond' => 'email#correspond'
resources :comments
resources :users
resources :subcomments
resources :profiles
resources :emails
resources :users do
resources :comments do
resources :subcomments
end
end
resources :comments do
resources :subcomments
end
resources :users do
resources :emails
resources :new_file
end
resources :users do
resources :emails do
end
end
resources :users do
resources :fiendships
end
resources :users do
resources :subcomments
end
end
Firstly, You don't need to redirect on same action. If you still need than simple check for id in set_user method. Like following
def set_user
#user = User.find(params[:id]) if params[:id]
end
From your routes.rb, you have:
post 'email/correspond' => 'email#correspond'
That is what is responsible for the route (url) that you have when you send the form : http://localhost:3000/email/correspond
However, you are interested in sending the id along with the request as a params (/:id) and not as a query (?id=:id), since you are looking for this from inside the email controller.
To achieve this, you have to configure your route to take the given id along, as follow:
post 'email/correspond/:id' => 'email#correspond'
Doing this will ensure the id is sent as params, and then when you call your set_user
def set_user
#user = User.find(params[:id])
end
User.find(params[:id]) looks inside the request's params, and takes whatever is in the place of /:id .
Example:
a post request to "http://localhost:3000/email/correspond/6" will give you params[:id] => 6
I have a model "User". I defined it in my routes.rb with resources :users. I want to be able to render different versions of the same user. To see if it would work, here's what I tried in my routes.rb:
get "users/:id_alt", :to => "users#alt", :as => :user
and in my controller:
def show
#user = User.find(params[:id])
end
def alt
#user = User.find(params[:id])
end
But when I navigated to users/1, I got this error:
ActiveRecord::RecordNotFound in UsersController#alt
Couldn't find User without an ID
and the error pointed to this line under def alt:
#user = User.find(params[:id])
Anyone know how to remedy this error or accomplish this another way?
It is because you have set your route to use :id_alt
get "users/:id_alt", :to => "users#alt", :as => :user
So the controller is assuming the value in that location should have the name :id_alt not :id. You are not going to be able to have both routes set up this way, but you could do this:
get "users/alt/:id_alt", :to => "users#alt", :as => :alt_user
get "users/:id", :to => "users#show", :as => :user
You will be able to use these path methods: alt_user_path and user_path
And your controller should look like this:
def show
#user = User.find(params[:id])
end
def alt
#user = User.find(params[:id_alt])
render :show
end
Running rake routes will result in this:
alt_user GET /users/alt/:id_alt(.:format) users#alt
user GET /users/:id(.:format) users#show
I am new to rails and I am trying to add a email confirmation upon register. I currently get this error.
(Bonus points for any verbose and easily understood answer.)
Routing Error
No route matches {:action=>"edit", :controller=>"email_activations", :id=>false}
config/routes.rb
LootApp::Application.routes.draw do
get "password_resets/new"
get "sessions/new"
resources :users
resources :sessions
resources :password_resets
resources :email_activations
root to: 'static_pages#home'
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "registered", :from => "alain#private.com")
end
end
app/controllers/email_activations_controller.rb
class EmailActivationsController < ApplicationController
def edit
#user = User.find_by_email_activation_token!(params[:id])
#user.email_activation_token = true
redirect_to root_url, :notice => "Email has been verified."
end
end
app/views/user_mailer/registration_confirmation.html.haml
Confirm your email address please!
= edit_email_activation_url(#user.email_activation_token)
resources keyword in rails routes is a magical keyword that creates 7 restful routes by default
edit is one of those
check these docs link
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
edit expects to edit a record so requires a id to find the record for editing
in your case
you can just add a custom action in users controller
like
in UsersController
def accept_invitation
#user = User.find_by_email_activation_token!(params[:token])
#user.email_activation_token = true
redirect_to root_url, :notice => "Email has been verified."
end
in routes.rb
resources :users do
collection do
get :accept_invitation
end
end
in app/views/user_mailer/registration_confirmation.html.haml
accept_invitation_users_url({:token=>#user.email_activation_token})
Check out how to add custom routes here
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
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.