Rails6: ArgumentError(form中的第一个参数不能包含nil或为空)



点击edit的下拉链接,错误为

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
1: = form_for [:admin, @tenant, @user] do |f|

在点击编辑按钮时请求的URL是http://localhost:3000/admin/tenants/2/users/3/edit(方法:get),以呈现用户编辑操作的编辑表单。

这是user_controller.rb的一部分

class Admin::UsersController < Admin::BaseController
before_action :set_tenant
def edit
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to [:admin, @tenant, @user], notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tenant
@tenant = Tenant.find(params[:tenant_id])
end

def set_user 
@user = User.find(params[:id])
end

def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :tenant_id, :unit_id)
end

app/views/admin/users/index.html.haml

= render(partial: 'admin/tenants/sublinks')
%h5.section-heading.mt20
Listing Users
.flex
= link_to 'New User', new_admin_tenant_user_path(@tenant), class: "waves-effect waves-light btn btn-small primary"
%table
%thead
%tr
%th Email
%th Default Unit
%th Created at
%th 
%th 
-# %th Delete User
%tbody
- @tenant.users.each do |user|
%tr
%td= user.email
%td= user.unit.name
%td= user.created_at
-# %td= link_to 'Delete', admin_tenant_user_path(@tenant, user.id), class: "waves-effect waves-red btn btn-small primary", method: :delete
%td
%a.dropdown-trigger.btn.waves-effect.waves-teal.btn-flat.right{"data-target" => "client_dropdown#{user.id}", :href => "#"}
%i.material-icons.right>expand_more
More
%ul.dropdown-content{id: "client_dropdown#{user.id}"}
%li
= link_to 'Edit', 
edit_admin_tenant_user_path(@tenant, user)
%li
= link_to 'Destroy', [:admin, @tenant, user], method: :delete, data: { confirm: 'Are you sure?' }

index.html.haml

= render(partial: 'admin/tenants/sublinks')
%h5.section-heading.mt20
Listing Users
.flex
= link_to 'New User', new_admin_tenant_user_path(@tenant), class: "waves-effect waves-light btn btn-small primary"
%table
%thead
%tr
%th Email
%th Default Unit
%th Created at
%th 
%th 
-# %th Delete User
%tbody
- @tenant.users.each do |user|
%tr
%td= user.email
%td= user.unit.name
%td= user.created_at
-# %td= link_to 'Delete', admin_tenant_user_path(@tenant, user.id), class: "waves-effect waves-red btn btn-small primary", method: :delete
%td
%a.dropdown-trigger.btn.waves-effect.waves-teal.btn-flat.right{"data-target" => "client_dropdown#{user.id}", :href => "#"}
%i.material-icons.right>expand_more
More
%ul.dropdown-content{id: "client_dropdown#{user.id}"}
%li
= link_to 'Edit', 
edit_admin_tenant_user_path(@tenant, user)
%li
= link_to 'Destroy', [:admin, @tenant, user], method: :delete, data: { confirm: 'Are you sure?' }

以下是路由信息:

admin_tenant_users GET        /admin/tenants/:tenant_id/users(.:format)                                                     admin/users#index
POST       /admin/tenants/:tenant_id/users(.:format)                                                     admin/users#create
new_admin_tenant_user GET        /admin/tenants/:tenant_id/users/new(.:format)                                                 admin/users#new
edit_admin_tenant_user GET        /admin/tenants/:tenant_id/users/:id/edit(.:format)                                            admin/users#edit
admin_tenant_user GET        /admin/tenants/:tenant_id/users/:id(.:format)                                                 admin/users#show
PATCH      /admin/tenants/:tenant_id/users/:id(.:format)                                                 admin/users#update
PUT        /admin/tenants/:tenant_id/users/:id(.:format)                                                 admin/users#update
DELETE     /admin/tenants/:tenant_id/users/:id(.:format)                                                 admin/users#destroy

我希望呈现表单视图并编辑用户。

ubuntu 20.04 LTS上的Rails -v: 6.0.6

在控制器的edit方法中传递表单所需的参数后,我自己解决了这个问题

def edit
:admin
set_tenant
set_user
end

虽然我不确定这是否是最佳实践,所以我仍然愿意接受答案和建议

最新更新