Home > Net >  Rails6 : ArgumentError(First argument in form cannot contain nil or be empty)
Rails6 : ArgumentError(First argument in form cannot contain nil or be empty)

Time:11-26

Upon hitting a dropdown link of edit, the error is

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

The URL which is getting requested on clicking edit button is http://localhost:3000/admin/tenants/2/users/3/edit (method : get) to render a edit form for user edit action.

Here is a part of 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

Here is 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?' }

Here is 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?' }

Here is the routes information :

                           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

I expect to render the form view and edit the user.

Rails -v : 6.0.6 on Ubunut 20.04 LTS

CodePudding user response:

I solved the problem myself after passing the form required arguments in the edit method of controller

  def edit
    :admin
    set_tenant
    set_user
  end

Although I am not sure if this is a best practise so I am still open to answers and suggestions

  • Related