我尝试在管理员控制器中创建用户但不运行它总是留下一些错误。
我使用application_helper使用devise登录我的root #home,但我不知道这是否是导致我在创建用户时出现更多问题的原因
应用程序/助手/ application_helper.rb:
module ApplicationHelper
def resource_name
:user
end
def resource_name_request
:request
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
app / controllers / application_controller.rb:应用控制器
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
def after_sign_in_path_for(resource)
session[:previous_url] ||
if current_user.role == "admin"
admin_index_path
end
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :role])
devise_parameter_sanitizer.permit(:account_update, keys: [:name, :country])
end
end
app / models / user.rb:用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
app / controllers / admin_controller.rb:Admin_controller
class AdminController < ApplicationController
before_action :authenticate_user!
def index
@user = User.new
end
def create
request_hash = {
:name => params[:name],
:email => params[:email],
:role => params[:role],
:password => params[:password],
:password_confirmation => params[:password_confirmation]
}
@user = User.new(request_hash)
@user.save
end
end
app / views / admin / index.html.erb:我现在用来创建的视图(目前我的问题来源)
<%= form_for @user, as: resource_name do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.select(:role) do %>
<% [['Admin', "admin"], ['User', "user"]].each do |c| -%>
<%= content_tag(:option, c.first, value: c.last) %>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
config / routes.rb:路由
Rails.application.routes.draw do
resources :admin
resources :accesses
devise_for :users, controllers: {
registrations: 'users/registrations'
}
root 'welcome#index'
错误:这是我当前的错误:
如果有人有更好的方法或帮助我的链接 - 我最近学习Rails所以任何阅读都会很好。
答案 0 :(得分:0)
未定义的方法users_path
默认情况下,form_for
与模型实例(即此处@user
)一起使用时会路由到相应的 restful route (即{ {1}}这里)。
我可以看到您正在尝试将其映射到users_path
,因此您需要明确提及admin#create
中网址的路径,如此
form_for
由此,您还需要更改<%= form_for @user, as: resource_name, url: admin_index_path do |f| %>
中的params
,因为参数将位于admin#create
哈希中,因此例如user
应更改为params[:name]