Hi does anyone know what is causing this error.
I'm new to rails but am guessing it has something to do with bootstrap. However, I have no idea how to fix it and I've been trying the last 2 hours.
Error
Template is missing
Missing template authentication/account_settings, application/account_settings with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/home/action/workspace/PAW/app/views" * "/home/action/.rvm/gems/ruby-2.1.1/gems/twitter-bootstrap-rails-2.2.8/app/views"
I have account_settings defined in the authentication controller
authentication_controller.rb
# Class written by Alan Dunne after following tutorials by Marc Clifton [Available # http://www.codeproject.com/Articles/575551/User-Authentication-in-Ruby-on-Rails#AdministratingUsers78]
class AuthenticationController < ApplicationController
def sign_in
#user = User.new
end
def login
username_or_email = params[:user][:username]
password = params[:user][:password]
if username_or_email.rindex('#')
email=username_or_email
user = User.authenticate_by_email(email, password)
else
username=username_or_email
user = User.authenticate_by_username(username, password)
end
if user
session[:user_id] = user.id
flash[:notice] = 'Welcome'
redirect_to '/home'
else
flash.now[:error] = 'Unknown user. Please check your username and password.'
# Assign user to instance variable for the `sign_in` view!
#user = User.new(params[:user])
render :action => "sign_in"
end
end
def signed_out
session[:user_id] = nil
flash[:notice] = "You have been signed out."
end
def new_user
#user = User.new
end
def register
#user = User.new(params[:user])
if #user.valid?
#user.save
session[:user_id] = #user.id
flash[:notice] = 'Welcome.'
redirect_to :root
else
render :action => "new_user"
end
end
def account_settings
#user = current_user
end
def set_account_info
old_user = current_user
# verify the current password by creating a new user record.
#user = User.authenticate_by_username(old_user.username, params[:user][:password])
# verify
if #user.nil?
#user = current_user
#user.errors[:password] = "Password is incorrect"
render :action => "account_settings"
else
# update the user with any new username and email
#user.update(params[:user])
# Set the old email and username, which is validated only if it has changed.
#user.previous_email = old_user.email
#user.previous_username = old_user.username
if #user.valid?
# If there is a new_password value, then we need to update the password.
#user.password = #user.new_password unless #user.new_password.nil? || #user.new_password.empty?
#user.save
flash[:notice] = 'Account settings have been changed'
redirect_to :root
else
render :action => "account_settings"
end
end
end
end
routes.rb
Rails.application.routes.draw do
resources :match_picks
resources :matches
root :to=>"home#index"
get "sign_in" => "authentication#sign_in"
# get "home" => "authentication#login"
# get "instructions" => 'home'
get "signed_out" => "authentication#signed_out"
get "new_user" => "authentication#new_user"
post "sign_in" => "authentication#login"
put "sign_in" => "authentication#login"
post "new_user" => "authentication#register"
put "new_user" => "authentication#register"
get "admin_users" => "admin#users"
delete "user/:id" => "admin#delete_user", :as => "user"
get "admin_users" => "authentication#admin_users"
get '/home', to: 'home#home'
get '/instructions', to: 'home#instructions'
get '/blocks', to: 'home#blocks'
post "match/create-match-pick" => "matches#create_match_pick", :as => :create_match_pick
get "account_settings" => "authentication#account_settings"
put "account_settings" => "authentication#set_account_info"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
The controller method:
def account_settings
#user = current_user
end
Is looking to render a template file located at:
views/authentication/account_settings.html.erb (or .jbuilder, etc)
Does that file exist? If not you should create it.
Your error says you're expecting a file named: account_settings.html.erb
Go to: app/views/authentication/, and search for the file.
You are missing a view.
It should be this file:/app/views/authentication/account_settings.html.erb
Related
I am getting this error saying undefined method update. The show and Edit buttons work well. It's just the update that isn't working. I am using friendly_id and the controller is scaffolded with better routing.
Here is my routes:
Rails.application.routes.draw do
resources :static_pages, except: [:show, :edit]
devise_for :users
mount Bootsy::Engine => '/bootsy', as: 'bootsy'
get 'profile' => "users#profile"
root 'indexes#index'
get ':slug', to: 'static_pages#show'
get ':slug/edit', to: 'static_pages#edit'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
Here is the static page controller for the update method:
class StaticPagesController < ApplicationController
before_action :set_static_page, only: [:show, :edit, :update]
before_action :static_page_params, only: [:update, :create]
before_action :destroy_static_page, only: :destroy
before_filter :except => [:show] do
redirect_to :new_user_session unless current_user && current_user.role == 5
end
# GET /static_pages
# GET /static_pages.json
def index
#static_pages = StaticPage.all
end
# GET /static_pages/1
# GET /static_pages/1.json
def show
end
# GET /static_pages/new
def new
#static_page = StaticPage.new
end
# GET /static_pages/1/edit
def edit
end
# POST /static_pages
# POST /static_pages.json
def create
#static_page = StaticPage.new(static_page_params)
respond_to do |format|
if #static_page.save
format.html { redirect_to #static_page, notice: 'Static page was successfully created.' }
format.json { render :show, status: :created, location: #static_page }
else
format.html { render :new }
format.json { render json: #static_page.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /static_pages/1
# PATCH/PUT /static_pages/1.json
def update
respond_to do |format|
if #static_page.update(static_page_params)
format.html { redirect_to #static_page, notice: 'Static page was successfully updated.' }
format.json { render :show, status: :ok, location: #static_page }
else
format.html { render :edit }
format.json { render json: #static_page.errors, status: :unprocessable_entity }
end
end
end
# DELETE /static_pages/1
# DELETE /static_pages/1.json
def destroy
#static_page.destroy
respond_to do |format|
format.html { redirect_to static_pages_url, notice: 'Static page was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_static_page
#static_page = StaticPage.find_by_slug(params[:slug])
end
def destroy_static_page
#static_page = StaticPage.find_by_slug(params[:slug])
end
# Never trust parameters from the scary internet, only allow the white list through.
def static_page_params
params.require(:static_page).permit(:title, :video_id, :content, :tags, :author, :date, :slug)
end
end
Here are the views that have been scaffolded for the edit page:
%h1 Editing static_page
= render 'form'
= link_to 'Show', #static_page
\|
= link_to 'Back', static_pages_path
Edit: It has to do with my routes. I can't get the show button to work on the fourm bellow. I try #static_page.slug but that takes me to /page_name/page_name. When I just do #static_page it takes me to /static_page/page_name. What I need it to do is take me to /page_name.
Edit 2: Here is the form partial:
= simple_form_for(#static_page) do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.input :video_id
.bootsy_text_area
= f.bootsy_area :content, :class => 'form-control', rows: 12
= f.input :tags
= f.input :author
= f.input :date
.form-actions
= f.button :submit
You're calling #static_page.update(static_page_params), but in this case, #static_page is nil.
You've defined #static_page as #static_page = StaticPage.find_by_slug(params[:slug]), but this will return nil if no page with that slug is found.
There are two ways you can handle this:
Use find_by_slug! instead, which will raise an exception if no record is found, or:
Check for the presence of nil yourself, and decide how you want to handle it.
I am having a hard time creating the routes to my pages. I have show which works but edit keeps popping up with an error. The current code is a modification of this railscast along with the friendly ID gem. Thank you if you can help.
Here is the controller:
class StaticPagesController < ApplicationController
before_action :set_static_page, only: [:show, :edit, :update, :destroy]
before_filter :except => [:show] do
redirect_to :new_user_session unless current_user && current_user.role == 5
end
# GET /static_pages
# GET /static_pages.json
def index
#static_pages = StaticPage.all
end
# GET /static_pages/1
# GET /static_pages/1.json
def show
end
# GET /static_pages/new
def new
#static_page = StaticPage.new
end
# GET /static_pages/1/edit
def edit
#static_page = StaticPage.find_by_slug(params[:slug])
end
# POST /static_pages
# POST /static_pages.json
def create
#static_page = StaticPage.new(static_page_params)
respond_to do |format|
if #static_page.save
format.html { redirect_to #static_page, notice: 'Static page was successfully created.' }
format.json { render :show, status: :created, location: #static_page }
else
format.html { render :new }
format.json { render json: #static_page.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /static_pages/1
# PATCH/PUT /static_pages/1.json
def update
respond_to do |format|
if #static_page.update(static_page_params)
format.html { redirect_to #static_page, notice: 'Static page was successfully updated.' }
format.json { render :show, status: :ok, location: #static_page }
else
format.html { render :edit }
format.json { render json: #static_page.errors, status: :unprocessable_entity }
end
end
end
# DELETE /static_pages/1
# DELETE /static_pages/1.json
def destroy
#static_page.destroy
respond_to do |format|
format.html { redirect_to static_pages_url, notice: 'Static page was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_static_page
#static_page = StaticPage.find_by_slug(params[:slug])
end
# Never trust parameters from the scary internet, only allow the white list through.
def static_page_params
params.require(:static_page).permit(:title, :permalink, :content, :tags, :author, :date)
end
end
Here is the route:
Rails.application.routes.draw do
resources :static_pages, except: [:show, :delete, :update]
devise_for :users
mount Bootsy::Engine => '/bootsy', as: 'bootsy'
get 'profile' => "users#profile"
root 'indexes#index'
get ':slug', to: 'static_pages#show'
get ':slug', to: 'static_pages#delete'
get ':slug/edit', to: 'static_pages#update'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
Here is the form where the error keeps poping up on form.html.haml:
= simple_form_for(#static_page) do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.input :permalink
.bootsy_text_area
= f.bootsy_area :content, :class => 'form-control', rows: 12
= f.input :tags
= f.input :author
= f.input :date
.form-actions
= f.button :submit
In your routes you have:
get ':slug/edit', to: 'static_pages#update'
Where you're sending a GET request to your update action, rather than your edit action.
Change that to:
get ':slug/edit', to: 'static_pages#edit'
You're also setting the same get :slug route to two different actions as well. The one pointing to the destroy action should be a DELETE request rather than a GET request.
One more thing, you can remove #static_page = StaticPage.find_by_slug(params[:slug]) from your controller edit action, since you're already setting the #static_page variable in your set_static_page in your before_action.
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
I'm getting a strange reaction to my 'destroy' method. I get this error when trying to destroy a project:
Unknown action
The action '5' could not be found for ProjectsController
I figured out when when I change my routes.rb file from resources :projects (plural) to resources :project (singular), the destroy action works as it should (and returns to the index) but then my show method works but update and new methods throw undefined methodprojects_path'` errors. Why is this happening??
class ProjectsController < ApplicationController
def index
#projects = Project.sorted
end
def show
#project = Project.find(params[:id])
end
def new
#project = Project.new
#project_count = Project.count + 1
end
def create
# Instantiate a new object using form parameters
#project = Project.new(project_params)
# Save the object
if #project.save
# If save succeeds, redirect to the index action
flash[:notice] = "Project created successfully."
redirect_to(:action => 'index')
else
# If save fails, redisplay the form so user can fix problems
#project_count = Project.count + 1
render('new')
end
end
def edit
#project = Project.find(params[:id])
#project_count = Project.count
end
def update
#project = Project.find(params[:id])
if #project.update_attributes(project_params)
flash[:notice] = "Project updated successfully."
redirect_to(:action => 'show', :id => #project.id)
else
#project_count = Project.count
render('edit')
end
end
def delete
#project = Project.find(params[:id])
end
def destroy
project = Project.find(params[:id])
project.destroy
flash[:notice] = "Project '#{project.name}' destroyed successfully."
redirect_to(:action => 'index')
end
private
def project_params
params.require(:project).permit(:name, :financing, :visible, :position)
end
end
routes.rb file:
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
match ':controller(/:action(/:id))', :via => [:get, :post]
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Exampl
e resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
Delete.html.erb page:
<% #page_title = "Delete Project" %>
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="project destroy">
<h2>Delete Projects</h2>
<%= form_for(:project, :url => {:action => 'destroy', :id => #project.id}) do |f| %>
<p>Are you sure you want to permanently delete this project?</p>
<p class="reference-name"><%= #project.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Project") %>
</div>
<% end %>
</div>
[UPDATE]
Another interesting update. I messed around with destroy and changed the controller to:
def delete
#project = Project.find(params[:id])
#project.destroy
end
def destroy
project = Project.find(params[:id])
project.destroy
flash[:notice] = "Project '#{project.name}' destroyed successfully."
redirect_to(:action => 'index')
end
I did this to see if it was an issue with the destroy method. It does destroy the project, and still returns the delete page which causes the same error, but it does destroy it.
Though you are doing a couple of unconventional stuff like having delete.html.erb (objects are deleted from the show view page), I'm going to address your direct question.
you said:
"when I change my routes.rb file from resources :projects (plural) to resources :project (singular), the destroy action works as it should (and returns to the index) but then my show method works but update and new methods throw undefined method projects_path'` errors. Why is this happening??"
you have understand every time you change your routes, you have to modify the helper paths in your views.
example: when you had resources :projects, you helper methods were (run rake db:migrate)
projects_path GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project_path GET /projects/new(.:format) projects#new
edit_project_path GET /projects/:id/edit(.:format) projects#edit
project_path GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
So at this point, projects_path was working because as you can see above, projects_path is there.
when you changed your routes to resources :project(singular), your helper paths change as well. run rake db:migrate to see them:
project_index_path GET /project(.:format) project#index
POST /project(.:format) project#create
GET /project/new(.:format) project#new
GET /project/:id/edit(.:format) project#edit
GET /project/:id(.:format) project#show
PATCH /project/:id(.:format) project#update
PUT /project/:id(.:format) project#update
DELETE /project/:id(.:format) project#destroy
As you can see the helper path that maps to project#index is project_index_path.
So I'm guessing in your edit.html.erb (or somewhere else) you have the old code: projects_path and so you are getting undefined method projects_path because the helper no longer exist. The correct helper method in this case, is project_index_path.
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