【问题标题】:ActionView::Template::Error (No route matches {:action=>"show", :controller=>"administration/roles" ... possible unmatched constraints: [:id]):ActionView::Template::Error(没有路由匹配 {:action=>"show", :controller=>"administration/roles" ...可能的不匹配约束:[:id]):
【发布时间】:2019-12-16 09:10:36
【问题描述】:

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

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'

控制器:

  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'

该错误是否与 Completed 401 Unauthorized 相关?

【问题讨论】:

  • 看起来它正在尝试使用角色对象而不是角色 ID 来构建 URL。或者给定的角色尚未持久化,请参阅错误中的 `:id=>#

标签: javascript ruby-on-rails ruby erb actionview


【解决方案1】:

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

由于new 操作中的角色未保存到数据库中 - 您不能为其创建后端销毁链接。

简而言之:

admin_role_path(@role)

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

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

不要使用后端删除链接 - 考虑在点击 link_to 'Delete' 时从页面中删除新建的对象,而不向后端抛出请求。

【讨论】:

  • 感谢您提供清晰的解释和解决方案。问题解决了
猜你喜欢
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2012-07-17
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
相关资源
最近更新 更多