我想从UsersController提取新的并创建动作到RegistrationsControllers(我刚刚制作),不幸的是出了点问题。
这是我的代码:
registrations_controller.rb:
class RegistrationsController < ApplicationController
def new
@user = User.new
end
def create
if current_user.is_admin
@user = User.new(admin_user_params)
else
@user = User.new(user_params)
end
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'Stworzono nowego użytkownika.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
users_controller.rb:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
skip_before_action :require_login, only: [:new, :create]
def index
if current_user.is_admin
@users = User.all
else
redirect_to root_path
end
end
def update
respond_to do |format|
if current_user.is_admin ? @user.update(admin_user_params) : @user.update(user_params)
format.html { redirect_to @user, notice: 'Zaktualizowano dane dt. użytkownika.' }
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
end
routes.rb中:
root :to => 'registration#index'
resources :user_sessions, only: [:create]
get 'login' => 'user_sessions#new', :as => :login
post 'logout' => 'user_sessions#destroy', :as => :logout
resources :tasks
resources :registrations
resources :projects do
get "search", on: :collection
resources :tasks
end
resources :users do
resources :tasks
end