Ruby on rails Rails:param缺失或值为空:admin
我正在从事一个Rails项目,我正在为模型和控制器使用名称空间。也就是说,子模型放在名为Ruby on rails Rails:param缺失或值为空:admin,ruby-on-rails,ruby,erb,Ruby On Rails,Ruby,Erb,我正在从事一个Rails项目,我正在为模型和控制器使用名称空间。也就是说,子模型放在名为user的目录中,控制器放在名为users的目录中 为了更清楚,我有一个用户模型,该模型使用单表继承(STI)具有子管理员和学生模型。这些子模型继承父模型的所有属性,可以单独定义它们的验证和方法,并放置在名为user的目录中 我还有admin\u controller和student\u controller,它们分别使用admin模型和student模型。这些控制器放置在名为users的目录中 这是我的代码
user
的目录中,控制器放在名为users
的目录中
为了更清楚,我有一个用户
模型,该模型使用单表继承(STI)具有子管理员
和学生
模型。这些子模型继承父模型的所有属性,可以单独定义它们的验证和方法,并放置在名为user
的目录中
我还有admin\u controller
和student\u controller
,它们分别使用admin
模型和student
模型。这些控制器放置在名为users
的目录中
这是我的代码
用户模型:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
index.html.erb:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
管理员
电子邮件
密码
角色
show.html.erb:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
电子邮件:
密码:
角色:
|
\u form.html.erb:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
禁止保存此管理员:
新建.html.erb:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
新管理员
edit.html.erb:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
编辑管理员
|
但是,当我尝试创建新管理员或更新现有管理员时,会出现以下错误:
参数丢失或值为空:admin
我已尝试调试问题的原因,但尚未找到解决方法。任何形式的帮助都将不胜感激。经过仔细检查日志,我终于找到了答案 以下是我解决问题的方法:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
我检查了创建
操作的请求参数,我意识到它的格式如下:
{"authenticity_token"=>"qJWlHz7Z5myTH3dwNIjjSOzRDY7JN+LoovaG+8dMBnGFRWImJKlWp8cgF7kwTqJXIxqU2fGVkqW9nhOAJ8vFIg==",
"user_admin"=>{"email"=>"promise@gmail.com", "password"=>"[FILTERED]", "role"=>""},
"commit"=>"Create Admin"}
因此,请求参数的散列键为user\u admin
,值为email
、password
和role
,而我是在admins\u控制器中传递admin
散列键的
我所要做的就是修改我的admins\u控制器中的admins\u参数
私有方法
:
由此:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
对此:
class User < ApplicationRecord
has_secure_password
end
class User::Admin < User
end
class User::Student < User
end
class Users::AdminsController < ApplicationController
before_action :set_admin, only: [:show, :edit, :update, :destroy]
# GET /admins
# GET /admins.json
def index
@admins = User::Admin.all
end
# GET /admins/1
# GET /admins/1.json
def show
end
# GET /admins/new
def new
@admin = User::Admin.new
end
# GET /admins/1/edit
def edit
end
# POST /admins
# POST /admins.json
def create
@admin = User::Admin.new(admin_params)
respond_to do |format|
if @admin.save
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully created.' }
format.json { render :show, status: :created, location: users_admin_path(@admin) }
else
format.html { render :new }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/1
# PATCH/PUT /admins/1.json
def update
respond_to do |format|
if @admin.update(admin_params)
format.html { redirect_to users_admin_path(@admin), notice: 'Admin was successfully updated.' }
format.json { render :show, status: :ok, location: users_admin_path(@admin) }
else
format.html { render :edit }
format.json { render json: @admin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/1
# DELETE /admins/1.json
def destroy
@admin.destroy
respond_to do |format|
format.html { redirect_to users_admins_url, notice: 'Admin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin
@admin = User::Admin.find(params[:id])
end
# Only allow a list of trusted parameters through.
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
end
Rails.application.routes.draw do
namespace :users do
resources :admins
resources :students
end
end
<p id="notice"><%= notice %></p>
<h1>Admins</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<td><%= admin.email %></td>
<td><%= admin.password %></td>
<td><%= admin.role %></td>
<td><%= link_to 'Show', users_admin_path(admin) %></td>
<td><%= link_to 'Edit', edit_users_admin_path(admin) %></td>
<td><%= link_to 'Destroy', users_admin_path(admin), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Admin', new_users_admin_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Email:</strong>
<%= @admin.email %>
</p>
<p>
<strong>Password:</strong>
<%= @admin.password %>
</p>
<p>
<strong>Role:</strong>
<%= @admin.role %>
</p>
<%= link_to 'Edit', edit_users_admin_path(@admin) %> |
<%= link_to 'Back', users_admins_path %>
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :role %>
<%= form.text_field :role %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<h1>New Admin</h1>
<%= form_with(model: @admin, url: users_admins_path, local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Back', users_admins_path %>
<h1>Editing Admin</h1>
<%= form_with(model: @admin, url: users_admin_path(@admin), local: true) do |form| %>
<%= render partial: 'form', admin: @admin, locals: { form: form } %>
<% end %>
<%= link_to 'Show', users_admin_path %> |
<%= link_to 'Back', users_admins_path %>
def admin_params
params.require(:admin).permit(:email, :password, :role)
end
def admin_params
params.require(:user_admin).permit(:email, :password, :role)
end
这就解决了问题
如果您有这样的问题,请务必检查日志或应用程序跟踪中的请求参数。这将为您提供大量信息,帮助您解决问题
就这些
我希望这有帮助