I am trying to create a log out for my app and in my controller this piece of code is not working. Any solution would be helpful.
def destroy
#user = user.find(params[:id])
#user.destroy
end
This is my destroy method in my sessions controller(the fact that i am destroying my user in my sessions controller might be the problem)
class UsersController < ApplicationController
def new
end
def create
#user = User.create(password: params[:password],
email: params[:email],
firstname: params[:firstname],
lastname: params[:lastname])
redirect_to user_path(#user)
end
def show
#user = User.find(params[:id])
end
end
routes.rb:
Rails.application.routes.draw do
# Fubyonrails.org/routing.html
root :to => 'static#welcome'
resources :users, only: [:new, :create, :show]
resources :session, :only => [:new, :create, :destroy]
resources :studios
end
Routes file
uninitialized constant SessionController
is the error im getting
First, don't destroy your user. It deletes it from the database, you want them to be able to login again without creating a new user right?
You need to update your routes.rb file to be:
Rails.application.routes.draw do
# Fubyonrails.org/routing.html
root :to => 'static#welcome'
resources :users, only: [:new, :create, :show]
resources :sessions, :only => [:new, :create, :destroy] # changed to sessions
resources :studios
get 'logout' => 'sessions#destroy'
end
If you don't already, you need to define a SessionsController.
class SessionsController < ApplicationController
def destroy
# insert logic that actually logs them out, the below may already be enough
# for this purpose though
redirect_to root_url, notice: "Logged Out Successfully"
end
end
When the user clicks "Logout" it gets routed to the SessionsController#destroy action which redirects them to the root URL ('static#welcome')
There is more to it then that (i.e. the views and all that jazz) but this should be helpful enough
Related
in routes.rb i have
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get 'account_activation/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'sign_up' => 'user#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
post 'sign_up', to: 'user#create'
resources :user
resources :account_activation, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :purchases, only: [:create, :destroy]
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
i define in user_controller
class UserController < ApplicationController
before_action :logged_in_user, only: [:show, :edit, :update]
before_action :correct_user, only: [:show, :edit, :update]
def show
#user = User.find_by(id: params[:id])
#purchases = #user.purchases.paginate(page: params[:page])
#purchase = current_user.purchases.build
end
def index
if logged_in?
redirect_to root_url
else
redirect_to sign_up_path
end
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
#user.send_account_activation_email
flash[:info] = 'check'
redirect_to root_url(#user)
else
render 'new'
end
end
def edit
end
def chart
end
def update
if #user.update_attributes(user_params)
flash[:success] = 'updated'
redirect_to #user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def correct_user
#user = User.find_by(id: params[:id])
redirect_to(root_url) unless current_user?(#user)
end
end
both of them have a view but when i type localhost:300/user/(ID)/chart in browser rails tells me no route matches found but user/(ID)/edit works
when i use rake routes
chart do not shows in routes
i see MHartl rubyonrails tutorial screen cast
he do everything like this
where is the problem?!
resources :user do
member do
get :chart
end
end
define chart action like that
read routes in rails guide and check member and collection
resources :user creates only 7 default routes check 2.2 http://guides.rubyonrails.org/routing.html
I am new in Ruby on Rails.
I want to run http://localhost:3000/admin/users to see users index page.
But when I run this link, it guide me to http://localhost:3000/admin/login.
Is there something wrongs with my route setting?
Rails.application.routes.draw do
get 'users/new'
get 'users/show'
if Rails.env.development?
mount LetterOpenerWeb::Engine, at: '/letter_opener'
end
root to: 'helps#top'
# admin login
get 'admin/login', to: 'admin/login#index', as: 'admin/login'
get 'admin/logout', to: 'admin/login#logout'
post 'admin/login/login'
get 'admin', to: 'admin/projects#index', as: 'admin_top'
namespace :admin do
resources :users, only: %i(index new create)
resources :projects do
resources :project_users
resources :project_comments
end
resources :images
resources :categories
resources :campanies
end
end
users_controller.rb
class Admin::UsersController < AdminController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
#users = User.all
end
def show
end
def new
#user = User.new
end
def edit
end
#Post /admin/projects
def create
#user = User.new(user_params)
if #user.save
flash[:notice] = 'User saved successfully'
redirect_to :back
else
flash[:alert] = #user.errors
binding.pry
render :new
end
end
def update
end
def destroy
end
private
def set_user
#user = User.find(params [:id])
end
def user_params
params.require(:user).permit(:campany_id, :name, :email, :password_digest, :profile, :prefecture_id, :address)
end
end
Thank you!
Your UsersControllers is under the admin namespace, that's to say you must be logged in order to access to this.
If you want to have access without validating the user is currently logged in, then you'll have to remove the constraint or verification to the controller or to make a new controller and index method which point to /admin/users but this time without the user verification.
That's:
# app/controllers/users_controller.rb
class UsersController < ApplicationController
...
def index
#users = User.all
end
...
end
# config/routes.rb
get '/users', to: 'users#index'
'/users' or '/admin/users' as you want to do it, but if you use the last one then any person must infer that's a restricted resource .
Why do I get the following, "undefined method `micropost_path'" in StaticPagesController , error, even though I copied Hartl's code verbatim?
Below is some of the code.
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :edit]
end
StaticPages Controller
class StaticPagesController < ApplicationController
def home
if logged_in?
#micropost = current_user.microposts.build
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
def help
end
def about
end
def contact
end
end
MicropostsController
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
#feed_items = []
render 'static_pages/home'
end
end
def destroy
#micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
#micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if #micropost.nil?
end
end
Two of the answers, on this website, to this issue were solved for other reasons.
For example writing
micropost
instead of
microposts
I don't have these grammatical errors here if I am not mistaken.. Any idea what other problem could be causing this?
P.S. Using cloud9 IDE while following tutorial.
In routes.rb I had the following resources for microposts
resources :microposts, only: [:create, :edit]
Instead,it is supposed to be
resources :microposts, only: [:create, :destroy]
The
:destroy
was missing.
Found this out after running 'rake routes'..
got this route
edit_micropost GET /microposts/:id/edit(.:format) microposts#edit
instead of
micropost DELETE /microposts/:id(.:format) microposts#destroy
This shows the routes with their corresponding actions that should be in the controller.
I recently integrated cancan (1.6.9) into my Rails (3.2.10) project for authorization, and I'm having an issue with manually loading a resource in a before_filter. Here's a brief description of my scenario.
In config/routes.rb, I have the following entries...
resources :users
match '/profile', :to => 'users#show'
Here's what my users_controller.rb looks like...
class UsersController < ApplicationController
before_filter :load_show_resource, :only => :show
load_and_authorize_resource
...
def show
end
...
private
def load_show_resource
#user = params[:id] ? User.find(params[:id]) : current_user
end
end
If my current_user's id is 1, this code will let me access localhost:3000/users/1 but not localhost:3000/profile.
The entry in my cancan ability.rb class that's blocking this access is seen below - it's the cannot section. If I comment out the cannot entry, both urls work.
...
can [:show, :update], User, :id => user.id
cannot [:show, :update, :destroy], User do |u|
u.site_id != user.site_id
end
....
Shouldn't cancan use the resource that's being manually loaded in the before_filter for the show action regardless of whether or not params[:id] is present?
Interestingly enough, if I modify my users controller (see below) to use skip_load_and_authorize_resource :only => :show and manually calling authorize! in the show action, both urls work. Also, if I remove cancan altogether both urls work.
class UsersController < ApplicationController
load_and_authorize_resource
skip_load_and_authorize_resource :only => :show
...
def show
#user = params[:id] ? User.find(params[:id]) : current_user
authorize! :show, #user
end
...
private
def load_show_resource
#user = params[:id] ? User.find(params[:id]) : current_user
end
end
So, my question is why does what's explained in the Override loading in the cancan documentation, not work in this situation?
Thanks!
# controller
class UsersController < ApplicationController
load_and_authorize_resource, :except => :show
# skip_load_and_authorize_resource :only => :show
...
def show
#user = params[:id] ? User.find(params[:id]) : current_user
authorize! :show, #user
end
...
end
# ability.rb
can [:show, :update, :destroy], User do |u|
u.id == user.id && u.site_id == user.site_id
end
I am facing a weird problem
when I am trying to destroy active record it is giving this error.
AbstractController::ActionNotFound at /photos/6
The action 'destroy' could not be found for PhotosController
here is what my controller look like
class PhotosController < ApplicationController
before_filter :authenticate_user!
....
....
def delete
#photo = current_user.photos.find(params[:id])
#photo.destroy
redirect_to photos_path
end
and if I perform the same actions using console it is working fine(the record is getting deleted successfully ).
This is my routes file look like.
resources :photos, :only => [:index, :show, :new, :create] do
post 'upload', :on => :collection
end
I know I have not included :destroy in resources please suggest me how to insert :destroy action to delete photos
if you have resource of photo then action name should be destroy, not delete. and if not then please check your routes.
Seven default action that are generated by scaffolding are follow
index
new
edit
create
update
show
destroy # not delete
I am assuming you are using resources :photo.
The action should be destroy and not delete, as per the Rais Guide.
The seven default actions are: index, new, create, show, edit, update and destroy
def destroy
#photo = current_user.photos.find(params[:id])
#photo.destroy
redirect_to photos_path
end
You can also see the available routes by:
rake routes
EDIT
The problem is here: :only => [:index, :show, :new, :create], which is saying to rails: do not create destroy, edit or update routes.
To solve the problem, you can add destroy to it :only => [:index, :show, :new, :create, :destroy] or use :except instead: :except => [:edit, :update]
If you don't want to limitate the resources:
resources :photos do
post 'upload', :on => :collection
end
EDIT 2 - I don't really understand why you are trying to use delete instead of destroy, However, if you have a good reason for it:
resources :photos :only => [:index, :show, :new, :create] do
post 'upload', :on => :collection
get 'delete', :on => :member
end
In this way you will have the delete_photo_path, which can be used in your show view:
<%= link_to 'Delete', delete_photo_path(#photo) %>
Finally, the action delete should looks like:
def delete
#photo = Photo.find(params[:id])
#photo.destroy
redirect_to photos_path
end