Rails Routing Error on Email Confirmation - ruby-on-rails

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

Related

Sending email, id is not passed

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

how to send confirmation email using rails?

I'm trying to setup logic in a rails 4.2.0 app where a person has to confirm their user account before they can login to the site / rails app. Basically, I have a sign up form where a person can input an email / password and their signed up. During this process an email is sent to their address with a confirmation token that should provide a link for them to confirm their account. I'm not exactly sure how to use the confirmation token so it changes a boolean value in the DB from false to true. I'll post what I have implemented so far.
users_controller.rb
def create
#user = User.new(user_params)
if #user.save
# send confirmation email after user has been created.
#user.send_confirmation
session[:user_id] = #user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
def confirm
#user = User.find_by_confirmation_token!(params[:id])
if #user.update_attributes(confirmed: true)
redirect_to login_path
end
end
confirmation.text.erb
To confirm your account, click the URL below.
<%= user_url(#user.confirmation_token) %>
<%= url_for(controller: 'users', action: 'confirm') %>
If you did not request your account creation, just ignore this email and your account will not be created.
routes.rb
Rails.application.routes.draw do
resources :articles do
resources :comments
end
get 'resume' => 'resume#index'
get 'signup' => 'users#new'
get 'login' =>'sessions#new'
get 'logout' => 'sessions#destroy'
# the below route led to a rails routing error
# get 'confirm' => 'users/:confirmation_token#confirm'
resources :users
resources :sessions
resources :password_resets
# route to hopefully get confirmation link working :-/
match '/users/:confirmation_token', :to => 'users#confirm', via: [:post, :get]
# test route
match 'users/foo', :to => 'users#foo', via: [:post, :get]
root "articles#index"
# Added below route for correct "resumé" spelling
get 'resumé', :to =>"resume#index"
# get 'about#index'
get 'about' => 'about#index'
get 'contact' => 'contact#contact'
resources :about
resources :contact
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
I ended up separating the confirmation logic into it's own controller, i.e. away from the users_controller.rb This allowed me to add the following line to my routes.rb
resources :confirmations
which allowed me to edit the confirmation.text.erb and put the following link in the email message,
<%= edit_confirmation_url(#user.confirmation_token) %>
thus when a person receives an email to confirm their account, it routes to the edit action of the confirmation controller, which the edit action calls the update action, and confirms the account. The controller looks like the following,
confirmations_controller.rb
class ConfirmationsController < ApplicationController
def new
end
def edit
#user = User.find_by_confirmation_token!(params[:id])
update
end
def update
# #user = User.find_by_confirmation_token!(params[:id])
if #user.confirmation_sent_at < 2.hours.ago
redirect_to new_confirmation_path, :alert => "Confirmation has expired."
# elseif #user.update_attributes(params[:user])
elsif #user.update_attributes(confirmed: true)
redirect_to root_url, :notice => "Your account has been confirmed."
else
render :new
end
end
end

Acts as follower Set Up

I'm trying to get acts_as_follower working but i'm missing something. A user should be able to Follow another User.
I added the Gem :
gem "acts_as_follower", '~> 0.2.0' #0.2.0 for Rails 4
In my User model i added :
acts_as_follower
acts_as_followable
The User Controller i created looks like this :
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def follow
#user = User.find(params[:id])
current_user.follow(#user)
redirect_to :back
end
end
My routes :
App::Application.routes.draw do
root 'screens#index'
devise_for :users
get 'u/:id' => 'users#show', as: :user
resources :screens, :path => 's' do
member do
get :like
get :unlike
end
end
get "pages/home"
get "about" => "pages#about"
get "users/show"
end
now in the routes i have to add the member :follow =>
member do
get :follow
end
But i'm stuck at this point. I tried a few variations and my link to follow a User is :
<%= link_to "Follow", follow_user_path(#user)%>
and this is giving me the error ->
undefined method `follow_users_path' for #<#<Class:0x007fb4da78e920>:0x007fb4db3facb8>
The Solution was to create a user resource, it works perfectly now =>
resources :users do
member do
get :follow
get :unfollow
end
end

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

Rails devise help routing error No route matches "/sessions/user"

When I login on my page I automatic go to the route: http://localhost:3000/sessions/user
And I get this error:
Routing Error
No route matches "/sessions/user"
I have created a controller named sessions_controller.rb in users folder here it is:
class Users::SessionsController < Devise::SessionsController
def new
redirect_to root_url, :notice => "You have been logged out."
end
def create
user = User.authenticate(params[:login], params[:encrypted_password])
if user
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in successfully."
else
flash.now[:alert] = "Invalid login or password."
render :action => 'new'
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "You have been logged out."
end
end
My route file:
Densidste::Application.routes.draw do
match 'user/edit' => 'users#edit', :as => :edit_current_user
devise_for :users, :controllers => { :sessions => "users/sessions" } do
get "login", :to => "devise/sessions#new"
get "opret", :to => 'users/users#new'
get "logud", :to => 'users/users#destroy'
end
resources :sessions
resources :users
devise_for :users, :controllers => { :sessions => "users/sessions" }
resources :aktivs
resources :taggingposts
resources :tags
resources :kommentares
resources :posts
end
(Old question but I ran into the same issue when setting up Devise, so hope this helps others)
Removing resources :sessions from the routes file should solve the problem.
For those who experiencing this issue with Devise 2.0 and Rails 3.2.1 and checked all the observations made by #Micah Alcorn but still facing the problem — restart your web server. Worked for me.
You don't have a root_url defined. It is still pointing to the static public/index.html. (edited out by Ryan Bigg)
devise_for :users is stated twice.
resources :users is unnecessary unless you have a RESTful controller handling destroy and index actions outside of devise.
Do you, in fact, have a "users" controller for that first edit action too? That should probably be in a custom Users::RegistrationsController < Devise::RegistrationsController.

Resources