简体   繁体   中英

ActionView::Template::Error (No route matches {:action=>“show”, :controller=>“administration/roles” … possible unmatched constraints: [:id]):

I am getting the following error on an action that was previously working. I'm trying to create new roles for an Admin user.

Started GET "/admin/roles/new" for 127.0.0.1 at 2019-08-08 10:15:18 -0500
   (1.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by Administration::RolesController#new as JS
  User Load (1.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", xxxxx], ["LIMIT", 1]]
  Role Load (1.0ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering administration/roles/new_role.js.erb
  Rendered administration/roles/_form.html.slim (31.0ms)
  Rendered administration/roles/new_role.js.erb (46.7ms)
Completed 401 Unauthorized in 169ms (ActiveRecord: 14.7ms)



ActionView::Template::Error (No route matches {:action=>"show", :controller=>"administration/roles", :id=>#<Role id: nil, name: nil, sell_tickets: false, hold_seats: false, issue_refunds: false, view_customers: false, view_admins: false, created_at: nil, updated_at: nil, landing_page: nil, edit_site: nil, refund_convenience_fees: false, release_seats: false, view_attendance_reports: false, view_account_reports: false>}, possible unmatched constraints: [:id]):
    11: td= check_box_for_role_attribute(@role, :view_customers)
    12: td= check_box_for_role_attribute(@role, :view_admins)
    13: td= submit_tag_for_role(@role)
    14: td= link_to 'Delete', admin_role_path(@role), method: 'DELETE', class: 'btn btn-danger btn-left'

app/views/administration/roles/_form.html.slim:14:in `_app_views_administration_roles__form_html_slim___1171376787447221819_70166561858320'
app/views/administration/roles/new_role.js.erb:2:in `_app_views_administration_roles_new_role_js_erb___2211564033647992023_70166561854740'
app/controllers/administration/roles_controller.rb:12:in `new'

controller:

  def new
    @role = Role.new
    render 'new_role.js.erb'
  end

new_role.js.erb

if($('tr[data-role-id="new"]').length === 0) {
  var new_role = $("<tr data-role-id='new'><%= j render('form')%><tr>");
  $('#roles_table tbody tr:first').before(new_role);
}

index.html.slim

.text-center 
    - if !@new_role
      = link_to 'Add Role', new_admin_role_path, class: 'btn btn-primary',
                                                 remote: true,
                                                 id: 'add_role'

Could the error be related to the Completed 401 Unauthorized ?

When you add a new role with javascript - Rails doesn't save anything to database and simply goes to new action, builds a new role object and renders it on the page.

Since role is not saved to the database in the new action - you can't have a backend destroy link for it.

So in short:

admin_role_path(@role)

in

link_to 'Delete', admin_role_path(@role), method: 'DELETE'

cannot be translated into a valid URL because @role does not exist in the database and thus doesn't have an id value and cannot be removed.

Instead of having backend deletion link - consider simply removing the newly built object from the page on click on link_to 'Delete' , without throwing request to the backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM