Please read at the very bottom, I edited my post, I still need help.
regards
I am using devise to authenticate the users and admin with (admin: true). As an admin I want to visit the users profile's pages but I always arrive on my own profile ( as the current_user). I don't know how to do...
Users could see others users profile too
Thanks for your help
users/index.html.slim
.container
h1 All the users
.row
table.board
thead
tr
th First Name
th Last Name
th Email Address
th Action on User
hr
tbody.board
-#users.each do |user|
.row
.col-xs-3
= user.first_name
.col-xs-3
= user.last_name
.col-xs-3
= user.email
.col-xs-1
#The problem is this link
= link_to 'View', user_path(user.id), class:'btn btn-success'
.col-xs-1
= link_to 'Remove', user_path(user), class:'btn btn-danger', method: :delete, data: {confirm: "Are you sure?"}
hr
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def show
#binding.pry
##user = User.find(current_user)
##user.id = User.find(params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos= Tuto.all
end
def index
if current_user.admin == true
#users = User.all
else
redirect_to root_path
end
end
def destroy
#user = User.find(params[:id])
#user.destroy
flash[:success] = "User was successfully deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :id)
end
end
The routes
#edited
Rails.application.routes.draw do
namespace :users do
resources :tutos
end
resources :tutos, only: [:show]
resources :tutos do
member do
put "like", to: "tutos#upvote"
end
end
get "/register", to: "devise/registrations#new", as: :register
get "/login", to: "devise/sessions#new", as: :login
get "/logout", to: "devise/sessions#destroy", as: :logout
get "/account", to: "users#show", as: :account
get "/login" , to: "devise/sessions#new", as: :new_user_session
post "/login" , to: "devise/sessions#create", as: :user_session
delete "/logout" , to: "devise/sessions#destroy", as: :destroy_user_session
devise_for :users, skip: [:sessions]
resources :users
root "home#landing"
end
edit rake routes gives :
$ rake routes
Prefix Verb URI Pattern Controller#Action
users_tutos GET /users/tutos(.:format) users/tutos#index
POST /users/tutos(.:format) users/tutos#create
new_users_tuto GET /users/tutos/new(.:format) users/tutos#new
edit_users_tuto GET /users/tutos/:id/edit(.:format) users/tutos#edit
users_tuto GET /users/tutos/:id(.:format) users/tutos#show
PATCH /users/tutos/:id(.:format) users/tutos#update
PUT /users/tutos/:id(.:format) users/tutos#update
DELETE /users/tutos/:id(.:format) users/tutos#destroy
like_tuto PUT /tutos/:id/like(.:format) tutos#upvote
tutos GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
new_tuto GET /tutos/new(.:format) tutos#new
edit_tuto GET /tutos/:id/edit(.:format) tutos#edit
tuto GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
account GET /account(.:format) users#show
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
root GET / home#landing
edit
after the last edits I still have a problem....
when I try to go on the account_path I have this error
Last edit
Just to remind you, I am using devise:
As a user logged in, if I want to see my own profile, I use
account_path (and this work well)
The link for visiting a user's profile page looks like this:
= link_to 'View', user_path(user)
but it looks like it point exactly like: account_path. ( So on my profile, not on the user I want to visit)
I am not sure what to use in my controller, if I use #user = User.find(user_params[:id]) || current_user or ##user = User.find(user_params[:id])
I have the following error:
ActionController::ParameterMissing in UsersController#show
param is missing or the value is empty: user
If I use #user = User.find(current_user).
I am redirected on my own profile each time....
def show
#binding.pry
##user = User.find(current_user)
##user = User.find(user_params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos = Tuto.all
end
This will set #user to current user or to requested user for admins
#user = current_user.admin? ? User.find(params[:id]) : current_user
In #show you could something like:
# Assuming params[:id] is the ID of the user's profile you're trying to view
def show
user_id = current_user.admin? ? params[:id] : current_user.id
#user = User.find(user_id)
end
The problem is in your route file remove 'as user do' blocks it makes some bizzare things :
user GET /users/:id(.:format) users#show
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
Do you want to use the account_path to view another user's profile, or could you go to /users/2 for the user with id of 2? That uses the show action in the controller and routes to the user path using the relevant id?
Related
Before I begin yes I know I need a edit and update function in the posts and threads controller, but the issue I have is with the forum_post.user details getting lost in the update and the thread duplicating posts after the update, so I removed the code entirely so I can get help solving the problem by posting the controllers themselves.
But you're going to need the routes, before I post it /forum/ is just a fake route to nest the forum_threads/posts in and does not exist outside of it's scope.
Rake Routes output
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
forum_thread_forum_posts GET /forum/forum_threads/:forum_thread_id/forum_posts(.:format) forum_threads/forum_posts#index
POST /forum/forum_threads/:forum_thread_id/forum_posts(.:format) forum_threads/forum_posts#create
new_forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/new(.:format) forum_threads/forum_posts#new
edit_forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/:id/edit(.:format) forum_threads/forum_posts#edit
forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#show
PATCH /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#update
PUT /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#update
DELETE /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#destroy
forum_threads GET /forum/forum_threads(.:format) forum_threads#index
POST /forum/forum_threads(.:format) forum_threads#create
new_forum_thread GET /forum/forum_threads/new(.:format) forum_threads#new
edit_forum_thread GET /forum/forum_threads/:id/edit(.:format) forum_threads#edit
forum_thread GET /forum/forum_threads/:id(.:format) forum_threads#show
PATCH /forum/forum_threads/:id(.:format) forum_threads#update
PUT /forum/forum_threads/:id(.:format) forum_threads#update
DELETE /forum/forum_threads/:id(.:format) forum_threads#destroy
import_users POST /users/import(.:format) users#import
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / forum_threads#index
Routes:
Rails.application.routes.draw do
devise_for :users
scope "/forum" do
resources :forum_threads do
resources :forum_posts, module: :forum_threads
end
end
resources :users do
collection do
post :import
end
end
root 'forum_threads#index'
end
Forum Threads Controller
class ForumThreadsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]
def index
#q = ForumThread.search(params[:q])
#forum_threads = #q.result(distinct: true)
end
def show
#forum_post = ForumPost.new
end
def new
#forum_thread = ForumThread.new
#forum_thread.forum_posts.new
end
def create
#forum_thread = current_user.forum_threads.new forum_thread_params
#forum_thread.forum_posts.first.user_id = current_user.id
if #forum_thread.save
redirect_to #forum_thread
else
render action: :new
end
end
def destroy
#forum_thread.destroy
redirect_to root_path
end
private
def set_forum_thread
#forum_thread = ForumThread.find(params[:id])
end
def forum_thread_params
params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end
end
Forum Posts Controller
class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
#forum_post = #forum_thread.forum_posts.new forum_post_params
#forum_post.user = current_user
if #forum_post.save
redirect_to forum_thread_path(#forum_thread, anchor: "forum_post_#{#forum_post.id}"), notice: "Successfully posted!"
else
redirect_to #forum_thread, alert: "Unable to save your post"
end
end
private
def set_forum_thread
#forum_thread = ForumThread.find(params[:forum_thread_id])
end
def forum_post_params
params.require(:forum_post).permit(:body)
end
end
I know the forum edit path for link_to will be edit_forum_thread_path or just correct me if I'm wrong, but it's the posts edit/delete path I need help with since that controller is nested under forum_threads and using the module forum_threads, I originally figured it would be edit_forum_threads_forum_posts_path but that wasn't it either last time I tried before I removed those functions.
It would be edit_forum_thread_forum_post_path based on your rake routes output.
I have a rails app with a bit tricky model as you see. App is able to create tasks for a given user.
I'm using the controller/form below. Obviously when I create task for sby I have to fill the :executor field since I'm gonna be the :assigner by default. Once the task is created, it's gonna be an :assigned_task from my point of view.
The form below works perfectly for the new/create action and edit.html.erb gets displayed as well with the right parameters, but I get the error (even if I try to change an assigned_task not an executed_task): "No route matches [PATCH] "/users/1/tasks"" when it comes to the update action.
I'm not sure if it's my form or controller that goes wrong. If I hit $rake routes everything looks fine.
I got 2 questions: 1. How can I make the update action work for :assigned_tasks, so :assigner could edit the tasks they assigned to sby? 2. This is the harder question: What should I do to have :executors be able to edit as well the tasks they got assigned to (:executed_tasks)?
task model:
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
user model:
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id"
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id"
Form for new and edit:
<%= form_for #task, url: user_tasks_path do |f| %>
Controller:
def new
#user = current_user
#task = Task.new
end
def create
#user = current_user
#task = Task.new(task_params)
if #task.save
flash[:success] = "Task saved!"
redirect_to user_tasks_path(current_user)
else
render action: :new
end
end
def edit
#user = current_user
#task = Task.find(params[:id])
end
def update
#user = current_user
#task = #user.task.find(params[:id])
if #task.update_attributes(task_params)
flash[:success] = "Task updated!"
redirect_to user_tasks_path(current_user)
else
render action: :edit
end
end
private
def task_params
params.require(:task).permit(:executor_id, :name, :content, :deadline).merge(assigner_id: current_user.id)
end
routes
Prefix Verb URI Pattern Controller#Action
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /about(.:format) static_pages#about
static_pages_help GET /help(.:format) static_pages#help
static_pages_privacypolicy GET /privacypolicy(.:format) static_pages#privacypolicy
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
complete_user_task PATCH /users/:user_id/tasks/:id/complete(.:format) tasks#complete
uncomplete_user_task PATCH /users/:user_id/tasks/:id/uncomplete(.:format) tasks#uncomplete
incoming_tasks_user_tasks GET /users/:user_id/tasks/incoming_tasks(.:format) tasks#incoming_tasks
outgoing_tasks_user_tasks GET /users/:user_id/tasks/outgoing_tasks(.:format) tasks#outgoing_tasks
completed_tasks_user_tasks GET /users/:user_id/tasks/completed_tasks(.:format) tasks#completed_tasks
user_tasks GET /users/:user_id/tasks(.:format) tasks#index
POST /users/:user_id/tasks(.:format) tasks#create
new_user_task GET /users/:user_id/tasks/new(.:format) tasks#new
edit_user_task GET /users/:user_id/tasks/:id/edit(.:format) tasks#edit
user_task GET /users/:user_id/tasks/:id(.:format) tasks#show
PATCH /users/:user_id/tasks/:id(.:format) tasks#update
PUT /users/:user_id/tasks/:id(.:format) tasks#update
DELETE /users/:user_id/tasks/:id(.:format) tasks#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / static_pages#home
I pulled it off. I still don't get why the previous one didn't work out and why this does. I used it for user.profile. It's true though that the profile was in one_to_one relationship with the user.
controller:
def edit
#user = current_user
#task = Task.find(params[:id])
end
def update
#user = current_user
#task = Task.find(params[:id])
if #task.update_attributes(task_params)
flash[:success] = "Task updated!"
redirect_to user_tasks_path(current_user)
else
render action: :edit
end
end
form:
<%= form_for ([#user, #task]) do |f| %>
I have User model and Role model.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :role
def has_role?(role_name)
self.role == Role.where(name: role_name).first
end
def add_role(role_name)
role = Role.where(name: role_name).first
self.role = role unless role.blank?
end
end
class Role < ActiveRecord::Base
belongs_to :user
end
For now, my app have 2 user roles; Admin & Member.
What I'm trying to do now is, giving ability to the Admin to change the member's role (from Admin to Member OR from Member to Admin) using select tag.
I have this inside my views/users/_form.html.erb
<div class="col-md-4">
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<p><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</p>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :email %><br>
<%= f.text_field :email, class: "form-control" %>
<%= f.label :role %>
<%= f.select :role_id, options_for_select(Role.all.map{|c| [c.name, c.id]}, f.object.role_id)%>
</div>
<%= f.submit 'Save user', :class => 'btn btn-primary' %>
<%= link_to 'Back', users_path, class: "btn btn-primary" %>
<% end %>
</div>
This is my User controller:
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
#users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/1/edit
def edit
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to users_path, notice: 'user was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'user was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :encrypted_password)
end
end
And this is my routes.rb:
Rails.application.routes.draw do
devise_for :users
root "orders#index"
resources :orders
resources :drinks
resources :foods
# link to change foods/drinks status from 0 (processing) to 1 (done)
get '/orders/:id/update_foods_status', to: 'orders#update_foods_status', as: :update_foods_status
get '/orders/:id/update_drinks_status', to: 'orders#update_drinks_status', as: :update_drinks_status
# kitchen foods orders
get 'kitchen_foods', to: 'kitchen_foods#index'
# kitchen drinks orders
get 'kitchen_drinks', to: 'kitchen_drinks#index'
# list of all users
get 'users', to: 'users#index', as: :all_users
# single user
get 'users/:id', to: 'users#show', as: :single_user
# edit user
get 'users/:id/edit', to: 'users#edit', as: :edit_user
end
UPDATE: This is what I get after run rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / orders#index
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PATCH /orders/:id(.:format) orders#update
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
drinks GET /drinks(.:format) drinks#index
POST /drinks(.:format) drinks#create
new_drink GET /drinks/new(.:format) drinks#new
edit_drink GET /drinks/:id/edit(.:format) drinks#edit
drink GET /drinks/:id(.:format) drinks#show
PATCH /drinks/:id(.:format) drinks#update
PUT /drinks/:id(.:format) drinks#update
DELETE /drinks/:id(.:format) drinks#destroy
foods GET /foods(.:format) foods#index
POST /foods(.:format) foods#create
new_food GET /foods/new(.:format) foods#new
edit_food GET /foods/:id/edit(.:format) foods#edit
food GET /foods/:id(.:format) foods#show
PATCH /foods/:id(.:format) foods#update
PUT /foods/:id(.:format) foods#update
DELETE /foods/:id(.:format) foods#destroy
update_foods_status GET /orders/:id/update_foods_status(.:format) orders#update_foods_status
update_drinks_status GET /orders/:id/update_drinks_status(.:format) orders#update_drinks_status
kitchen_foods GET /kitchen_foods(.:format) kitchen_foods#index
kitchen_drinks GET /kitchen_drinks(.:format) kitchen_drinks#index
all_users GET /users(.:format) users#index
single_user GET /users/:id(.:format) users#show
edit_user GET /users/:id/edit(.:format) users#edit
My problem:
When I navigate to /users/1/edit, I get this error:
NoMethodError in Users#edit
undefined method `user_path' for #<#<Class:0x007fb702901c88>:0x007fb70098aaf8>
How do I fix this problem?
I found this answer but I couldn't understand it very clearly. Would appreciate a better explaination and answer.
Note: I'm using Devise gem.
You have in your routes file:
devise_for :users
which adds:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
Now what you want to do is allow admin to edit the users. For this you have defined:
# list of all users
get 'users', to: 'users#index', as: :all_users
# single user
get 'users/:id', to: 'users#show', as: :single_user
# edit user
get 'users/:id/edit', to: 'users#edit', as: :edit_user
which generates:
all_users GET /users(.:format) users#index
single_user GET /users/:id(.:format) users#show
edit_user GET /users/:id/edit(.:format) users#edit
Now in the form you are writing <%= form_for(#user) do |f| %> which by default is searching for user_path. So instead of the different routes you are defining just add:
resources :users
which will generate all the necessary routes you require and map the form to the correct path. This will save a lot of overhead like using only this <%= form_for(#user) do |f| %> will take you to the create action when the user is a new object and it will take you to the update action if the user is a persisted object.
Or if you need to user your routes then you need to do:
<%= form_for(#user, url: edit_user_path, method: :get) do |f| %>
But it always a bad idea to use GET method in form submission. These links might help you:
When should I use GET or POST method? What's the difference between them?
When do you use POST and when do you use GET?
Update:
As you need to assign a single role to the user from multiple it is better to use a select box like this:
f.collection_select(:role_id, Role.all, :id, :name)
Assuming that the user table has attribute role_id and the role table has attribute name. So this will allow you to assign a role to user. What you are printing is user.role which will always print the association instead of the role name.
Hope this helps.
Try using edit_user_path instead of user_path if you want to link to the edit page. If you take a look at your routes.rb file there is no route for user_path.
I'm building a web app that uses devise. Upon signup with :email, :password, and :password confirmation, the new user is redirected to a second form where they will enter social information (such as hobbies, age, location, etc...) right now the form only contains fields for first and last name.
After submitting the second form, the user should be redirected back to the users#index page which is also where users who are signing in are redirected to. I just created the second form, and after submitting I get the following error:
No route matches [POST] "/users/user/edit_profile"
Below are the relevant files. I'm pretty new, does anyone have any ideas about this?
routes.rb
Appname::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get 'register', to: 'devise/registrations#new'
get 'login', to: 'devise/sessions#new', as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
end
resources :users do
member do
get 'edit_profile'
end
end
root to: "home#index"
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/legal', to: 'static_pages#legal', via: 'get'
end
users_controller.rb
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
end
def create
end
def edit
end
def update
#user = User.find(params[:id])
#user.update!(user_params)
redirect_to #user
end
def destroy
end
def edit_profile_user
#user = User.find(params[:id])
redirect_to #user
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :hobbies)
end
end
edit_profile.html.erb
<h2>Tell us about yourself</h2>
<%= form_for(resource, as: resource_name, url: edit_profile_user_path(resource_name)) do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
<div><%= f.submit "Update" %></div>
<% end %>
here are my rake routes:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
edit_profile_user GET /users/:id/edit_profile(.:format) users#edit_profile
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / home#index
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
help GET /help(.:format) static_pages#help
legal GET /legal(.:format) static_pages#legal
change:
resources :users do
member do
post 'edit_profile'
end
end
use edit_profile_user_path(#user) instead.
In the Devise login-page, the value of resource_name is just a string "user"... you want to be pointing at an actual user's page, for that you need an actual user instance.... which for Devise's login page is what is stored in resource... but after you're logged in, it's stored in current_user.
However, your code seems to indicate that you're putting user into #user - so that's what I'd use.
I'm receiving the following error in my Rails app when I try to access a page that contains a form to create a post. I'm trying to implement a feature similar to Michael Hartl's Micropost feature in his sample app:
NoMethodError in Home#index
undefined method `posts_path' for #<#<Class:0xb5c70744>:0xb60013b8>
Here's the index view page that contains the code to insert the form:
<%= render 'shared/post_form' if user_signed_in? %>
_post_form.html.erb:
<%= form_for(#post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Provide your network with a status update..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
Here is the Home controller:
class HomeController < ApplicationController
before_filter :authenticate_user!
def index
#render :text => "Welcome #{current_user.email}!"
#users = User.all
#post = current_user.posts.build if signed_in?
end
end
I can really use some help in reviewing the code. I'm staring at it and I need someone else to review it for me. I'm new to Rails so please forgive me if I did not provide the necessary information.
Additional information: I'm using the Devise gem to handle user authentication.
Thanks!
EDIT: I added the wrong controller.
EDIT 2:
Routes.rb file:
AppName::Application.routes.draw do
#get "users/index"
#get "users/show"
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users do
member do
get :following, :followers, :posts
end
end
resources :works
resources :relationships, only: [:create, :destroy]
end
EDIT 3: Rake routes
root / home#index
root / home#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
posts_user GET /users/:id/posts(.:format) users#posts
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
works GET /works(.:format) works#index
POST /works(.:format) works#create
new_work GET /works/new(.:format) works#new
edit_work GET /works/:id/edit(.:format) works#edit
work GET /works/:id(.:format) works#show
PUT /works/:id(.:format) works#update
DELETE /works/:id(.:format) works#destroy
relationships POST /relationships(.:format) relationships#create
relationship DELETE /relationships/:id(.:format) relationships#destroy
You need to add resources :posts in your routes.rb file in order for Rails to automatically create the posts_path helper for you.
Adding resources :posts will generate the proper RESTful routes for you to create, delete, update, and fetch posts. Take a look at the Ruby on Rails Guide for routing, specifically this section here on routing and RESTful routes.