我有一个 users 表 (Devise) ,其中有一列“admin”(布尔值)来限定我的用户。我还有一个命名空间“后端”。我希望我的管理员用户可以从后端命名空间创建新用户。
所以我创建了一个Backend::UsersController
:
class Backend::UsersController < ApplicationController
layout 'admin'
before_filter :authenticate_user!
def index
@auteurs = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @auteurs }
end
end
def new
@auteur = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @auteur }
end
end
def create
@auteur = User.new(params[:auteur])
respond_to do |format|
if @auteur.save
format.html { redirect_to @auteur, notice: 'Article was successfully created.' }
format.json { render json: @auteur, status: :created, location: @auteur }
else
format.html { render action: "new" }
format.json { render json: @auteur.errors, status: :unprocessable_entity }
end
end
end
end
这是“新”视图中调用的“_form”部分:
<%= simple_form_for(@auteur) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
这是我的路线:
namespace :backend do
resources :articles
root to: "articles#index"
resources :accueil
resources :users
get "users/index"
get "users/create"
get "users/new"
end
devise_for :users
但是当我尝试访问“后端/用户/新”时,它驱使我出现这个错误:
NoMethodError in Backend/users#new
Showing C:/Ruby/acturevue/app/views/backend/users/_form.html.erb where line #1 raised:
undefined method `users_path' for #<#<Class:0x39616b0>:0x416f6d0>
有人对问题的根源有任何想法吗?谢谢
更新
所以,我修改了我的代码:
在 Backend::UsersController 中:
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user}
end
end
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to backend_users_path(@user), notice: 'Article was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
通知 :
<%= simple_form_for(@user) do |f| %>
但我仍然有错误。但是 backend_users_path 存在于 y 个路由中:
backend_users GET /backend/users(.:format) backend/users#
index
POST /backend/users(.:format) backend/users#
create
new_backend_user GET /backend/users/new(.:format) backend/users#
new
edit_backend_user GET /backend/users/:id/edit(.:format) backend/users#
edit
backend_user GET /backend/users/:id(.:format) backend/users#
show
PUT /backend/users/:id(.:format) backend/users#
update
DELETE /backend/users/:id(.:format) backend/users#
destroy