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.
all_users_path. Runrake routesin your console and post its results.users_pathspecific. Furthermore, there is not apost,put, orpatchroute either, so the form is looking for a non-existent path.