我尝试在我的管理控制器中创建用户但不运行它总是会留下一些错误。
我使用 application_helper 登录到我的 root # home with devise 但我不知道这是否是创建用户时导致我更多问题的原因
应用程序/助手/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,所以任何阅读都会很好。
未定义的方法 users_path
默认情况下,form_for
当与模型实例(即@user
此处)一起使用时,会路由到其相应的静态路由(即users_path
此处)。
我可以看到您正在尝试将其映射到admin#create
,因此您需要明确提及 url 的路径,form_for
例如
<%= form_for @user, as: resource_name, url: admin_index_path do |f| %>
那样,您还需要更改您的params
inadmin#create
因为参数将在user
哈希内,因此例如params[:name]
应该更改为params[:user][:name]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句