在其他控制器和视图中创建用户(Rails 4 with Devise)
问题描述:
我尝试在我的管理控制器中创建用户,但不运行它总是会留下一些错误。在其他控制器和视图中创建用户(Rails 4 with Devise)
我用application_helper登录到我的根#家色器件,但我不知道这是否是创建用户
应用程序/佣工/ 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
应用程序/控制器/ application_controller.rb:Aplication控制器
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
应用/模型/ 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
应用程序/控制器/ 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
应用程序/视图/管理/ 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 %>
配置/ routes.rb中:路线
Rails.application.routes.draw do
resources :admin
resources :accesses
devise_for :users, controllers: {
registrations: 'users/registrations'
}
root 'welcome#index'
该错误:这是我目前的错误:
如果有人有更好的方式来做到这一点或一个链接来帮助我 - 我最近学习Rails的,因此任何阅读将是一件好事。
答
undefined method 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
在admin#create
作为PARAMS将是一个user
哈希里面,所以例如params[:name]
应改为params[:user][:name]
感谢您回应,但现在产生这个错误:::的ActionView ::模板错误(没有路由匹配{:行动= >“show”,:controlle r =>“admin”}缺少必需的密钥:[:id]): –
https://framapic.org/2NnnEJ7s3Zsc/JLJMtRlCUkN3.jpg –
@ J.leo道歉应该是'admin_index_path'。更新了我的回答 – Pavan