ActionView::Template::Error(没有路由匹配 {:action=>"show", :controller=>"administration/roles" ...可能不匹配的约束:[:id]):

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

我在之前有效的操作中收到以下错误。我正在尝试为管理员用户创建新角色。

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" =  ORDER BY "users"."id" ASC LIMIT   [["id", xxxxx], ["LIMIT", 1]]
  Role Load (1.0ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."id" =  LIMIT   [["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'

控制器:

  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'

错误可能与已完成的 401 未授权有关吗?

当您使用 javascript 添加新角色时 - Rails 不会将任何内容保存到数据库,而只是转到 new 操作,构建一个 new 角色对象并将其呈现在页面上。

由于角色没有在 new 操作中保存到数据库中 - 您不能为它设置后端销毁 link。

简而言之:

admin_role_path(@role)

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

无法转换为有效的 URL,因为 @role 在数据库中不存在,因此没有 id 值且无法删除。

而不是后端删除 link - 考虑在单击 link_to 'Delete' 时从页面中简单地删除新建对象,而不向后端发出请求。