I have a user controller as follows. I want to use the best_in_place gem for editing name and bio in profile page. I have followed Ryan Bates railscast but its not working correctly. When I tried to see the error through the chrome inspector I see best in place is requesting url http://localhost:3001/user/3 and now throwing 404 not found error.
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!, only: [:profile]
respond_to :json, :html
def profile
#user = current_user
end
def show
#user = User.find(params[:id])
end
def update
if current_user.update(user_params)
respond_with current_user
end
end
def edit
#user = current_user
end
private
def user_params
params.require(:user).permit(:name, :bio)
end
end
routes.rb
Rails.application.routes.draw do
resources :user
end
In routes.rb you configured routes as resources :user which gives following routes:
But we can see your controller is UsersController so you have to configured your routes as: resources :users Which gives following routes:
Hope this may solve your problem.
Related
Problem: I am using the after_sign_up_path_for(resource) method provided by Devise but I cannot seem to get the User redirected to the specified page after they sign up. Currently I receive a Template Missing error with the url being www.localhost:3000/users. Ideally, I would like to have the User redirected to www.localhost:3000/subscribers/new
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
#user = User.new(sign_up_params)
#domain = #user.email.split('#').last
#company = Company.find_by_domain(#domain)
#user.company_id = #company.id
if #user.save
after_sign_up_path_for(#user)
else
render 'new'
end
end
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :participation_code, :company_id)
end
protected
def after_sign_up_path_for(resource)
"/subscribers/new"
end
end
routes.rb
...
devise_for :users, controllers: { registrations: "registrations" }
...
Error Log I Get:
Template is Missing
Missing template registrations/create, devise/registrations/create,
devise/create, application/create with {:locale=>[:en], :formats=
[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby,
:coffee, :arb, :haml, :jbuilder]}.
UPDATE: I have tried moving the after_sign_up_path_for(resource) to the ApplicationController but still got the same result.
Very interesting. I simply changed
def create
...
end
to...
def update
...
end
Now everything works perfectly and my after_sign_up_path_for(resource) method is recognized.
Add below method in your application controller
Devise: Where to redirect users once they have logged in
def after_sign_up_path_for(resource)
"http://www.google.com" # <- Path you want to redirect the user to.
end
Maybe it worth to put redirect_method in after_action : create callback?
you dont need to create controller you can redirect user to specific path by defining
def after_sign_in_path_for(resource)
your_path
end
remember use after_sign_in_path_for not after_sign_up_path_for
I have a users_controller.rb that includes:
before_action :set_user, only: [:show, :edit, :update, :destroy]
load_and_authorize_resource
def index
#users = User.all
end
def show
end
def new
#user = User.new
end
private
def set_user
#user = User.find(params[:id])
end
My config/routes includes:
devise_for :users
devise_scope :user do
authenticated :user do
root 'switchboard#show', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
resources :users
When I navigate to http://localhost:3000/users I get a list of the users as I expect. But when I run the RSpec test:
require 'rails_helper'
RSpec.describe UsersController, :type => :controller do
describe "GET show" do
it "returns http success" do
get :show
expect(response).to be_success
end
end
end
I get the error No route matches {:action=>"show", :controller=>"users"} on the RSpec line get :show.
I've found countless queries about No route matches {:action=>"show", each one with a different cause, but none of them seems to match mine. What do I do to fix this (other than not test the feature!)
Because you have no route that matches controller user action show
However, you do have a route that matches user/:id (it's the :id you're missing)
So you could do...
get :show, id: 1
I am using RoR 4 and the Devise gem for user authentication. I want a user to be able to edit their own content and not anyone else's content. The authenticate_user method only seems to make sure the user is logged in before they can edit the content. But another user can just sign up and edit everyone else's content.
My controller looks like:
class PrayersController < ApplicationController
before_action :find_prayer, only: [:show, :edit, :updated, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#prayers = Prayer.all.order("created_at DESC")
end
def show
end
def new
#prayer = current_user.prayers.build
end
def edit
end
def create
#prayer = current_user.prayers.build(prayer_params)
if #prayer.save
redirect_to #prayer, notice: "Successfully created prayer"
else
render 'new'
end
end
def update
#prayer = Prayer.find_by_id(params[:id])
if #prayer.update(prayer_params)
redirect_to #prayer, notice: "Prayer was successfully updated"
else
render 'edit'
end
end
def destroy
#prayer.destroy
redirect_to root_path
end
private
def prayer_params
params.require(:prayer).permit(:title, :body)
end
def find_prayer
#prayer = Prayer.find(params[:id])
end
end
I tried to make a before_action of my own that looked something like this:
def own_prayer
if !current_user == Prayer.current_user
redirect_to #prayer, notice: "You cannot edit this prayer"
end
end
But that did not work. I can limit access to the form via the view with a similar action but I don't think this is entirely safe?
Thank you
I guess you don't have class method current_user on Prayer class. Also seems you have has_many :prayers in your User model. So to get prayer's user you need to call user method on Prayer instance variable.
It's supposed to be like that:
#prayer = Prayer.find params[:id]
unless current_user == #prayer.user
redirect_to(#prayer, notice: "You cannot edit this prayer") and return
end
If you need more tricky restriction rules then use cancan gem
I am tryting to add this method to a restful controller (also has working methods for :index, :new, :create, :edit, :update and :destroy):
class Admin::ClassSectionsController < ApplicationController
def clone
#class_section = ClassSection.find(params[:id])
#class_section = ClassSection.new(#class_section.attributes)
render :new
end
end
And the following for the config/routes.rb:
Utg::Application.routes.draw do
devise_for :users
root to: "home#index"
namespace :admin do
resources :class_sections, except: [:show]
end
resources :class_sections, only: [:index, :show]
end
How do I add the route for 'admin/class_section/:id/clone'?
namespace :admin do
get "class_sections/:id/clone", to: "class_sections#clone", as: "class_sections_clone"
end
in config/routes.rb, I have:
root to: "pages#home"
get "/visitor" => "pages#visitor"
In my controllers/application_controller.rb, I have:
before_filter :route_relative_to_login_state_and_role
private
def route_relative_to_login_state_and_role
if current_user
redirect_to root_path
else
redirect_to visitor_path
end
end
In my controllers/pages_controller.rb, I have:
class PagesController < ApplicationController
before_action :authenticate_user!, :only => [:home]
def home
#page_title = t ('home_page_title')
end
def visitor
#page_title = t ('visitor_page_title')
end
end
Here's my views/pages/home.html.erb
<h1><%= #page_title %></h1>
Here's my views/pages/visitor.html.erb
<h1><%= #page_title %></h1>
This is from the output of rake routes:
visitor GET /visitor(.:format) pages#visitor
When I start the server and point my browser to htp://localhost:3000 the browser tells me that the page is not redirecting properly. Here's what the log file says:
Started GET "/visitor" for 127.0.0.1 at 2014-05-15 10:37:46 -0700
Processing by PagesController#visitor as HTML
Redirected to http://localhost:3000/visitor
Filter chain halted as :route_relative_to_login_state_and_role rendered or redirected
if i understand you correctly if the user logged_in you what to redirect to page and if not to another page, what you did is wrong and you can control it from ApplicationController and do something like:
before_filter :check_user
def check_user
if current_user
## this is logged user
redirect_to first_path
else
## this is unlogged user
redirect_to second_path
end
end
hope that's what you meant.