Rails 4 Devise, Pundit, Join Table ActionView::Template::Error (#<#<Class:0x37682f95>:0x691



这是我的用例:

  1. 我有一个用户模型与Devise的AuthN和我使用Pundit的AuthZ
  2. 我通过子域约束来限制对主应用程序的访问
  3. 我有一些页面是为最终用户准备的(在某个时候会是一个不同的LandF),我有管理页面等
  4. 我使用的是has_and_belongs_to_many,它使用带有:id的联接表
  5. 我将我的控制器、视图目录和迁移命名为复数,将我的模型命名为单数。示例:TaskDefinition=>Model,TaskDefinitions=>Controller and Tables
  6. 默认的路线是生成的,我已经提供了内容
  7. 我在视图目录中使用分部,这是一个非常新的问题,因为从Ruby到JRuby的端口

堆栈跟踪:

ActionView::Template::Error (undefined method `task_definition_path' for #<#<Class:0x37682f95>:0x6919b2b2>):
    10:     <div class="col-md-10">
    11:       <div class="panel-body">
    12:         <div class="form">
    13:           <%= bootstrap_form_for @task do |f| %>
    14:               <div class="form-group">
    15:                 <%= render '/admin/task_definitions/errors' %>
    16:               </div>
  app/views/admin/task_definitions/edit.html.erb:13:in`_app_views_admin_task_definitions_edit_html_erb__1276994696_33458'

迁移:

class CreateTaskDefinitions < ActiveRecord::Migration
  def change
    create_table :task_definitions do |t|
      # foreign key
      t.integer :organization_id
      # attributes
....
      t.timestamps
    end
    # index
    add_index :task_definitions, :name, unique: true
  end
end
class CreateOrganizations < ActiveRecord::Migration
  def change
    create_table :organizations do |t|
      # for the relationship between parent orgs and child nodes
      t.references :parent
      # Used to determine Parent or Child
      t.string :org_type
      # Subdomain used for scoping site
      t.string :subdomain
      # Common fields
....
      t.timestamps
    end
    # index
    add_index :organizations, [:name, :subdomain], unique: true
  end
end
class CreateOrganizationsTaskDefinitions < ActiveRecord::Migration
  def change
    create_table :organizations_task_definitions, id: false do |t|
      t.integer :organization_id
      t.integer :task_definition_id
    end
    add_index :organizations_task_definitions, [:organization_id, :task_definition_id], name: 'index_organizations_task_definitions'
  end
end

型号:

class Organization < ActiveRecord::Base
  #associations
  has_many :users, class_name: 'User', inverse_of: :organization
  has_and_belongs_to_many :task_definitions, class_name: 'TaskDefinition', inverse_of: :organizations
  has_one :address, class_name: 'Address'
  has_many :children, class_name: 'Organization', foreign_key: 'parent_id'
  belongs_to :parent, class_name: 'Organization'
  accepts_nested_attributes_for :address
end
class TaskDefinition < ActiveRecord::Base
  #associations
  has_many :steps, class_name: 'TaskStep', inverse_of: :task_definition
  has_and_belongs_to_many :organizations, class_name: 'Organization', inverse_of: :task_definitions
  has_and_belongs_to_many :task_events, class_name: 'TaskEvent', inverse_of: :task_definitions
  accepts_nested_attributes_for :steps
end

控制器:

class Admin::TaskDefinitionsController < ApplicationController
  before_filter :authenticate_user!
  after_action :verify_authorized
.....
  def edit
    @tasks = current_organization.task_definitions
    if(@tasks.size > 0 )
      @task = @tasks.find(params[:id])
      authorize @task
      # add breadcrumb
      add_breadcrumb @task.name, admin_task_definition_path(@task)
      unless current_user.org_super_admin? or current_user.finch_admin?
        unless @user == current_user
          redirect_to :back, :alert => "Access denied."
        end
      end
    end
  end
end

路线:

Rails.application.routes.draw do
 ......
  constraints(Finch::Constraints::SubdomainRequired) do
    #
    # dashboards
    #
    resource :dash_boards, only: [:index, :show, :edit, :update, :destroy]
    #
    # orgss
    #
    resource :organizations, only: [:index, :show, :edit, :update, :destroy]
    #
    # Only Admins are allowed to access
    #
    namespace :admin do
      #
      # Workflow Data
      #
      resources :task_definitions, only: [:index, :show, :edit, :update, :destroy]
      resources :task_steps, only: [:show, :edit, :update, :destroy]
      resource  :task_actions, only: [:show, :edit, :update, :destroy]
      resource  :task_action_attributes, only: [:show, :edit, :update, :destroy]
      resource  :task_transitions, only: [:show, :edit, :update, :destroy]
    end
  end
end

视图:

    <div class="form">
      <%= bootstrap_form_for @task do |f| %>
          <div class="form-group">
            <%= render '/admin/task_definitions/errors' %>
          </div>

耙式路线:

           edit_organizations GET    /organizations/edit(.:format)                organizations#edit
                organizations GET    /organizations(.:format)                     organizations#show
                              PATCH  /organizations(.:format)                     organizations#update
                              PUT    /organizations(.:format)                     organizations#update
                              DELETE /organizations(.:format)                     organizations#destroy
       admin_task_definitions GET    /admin/task_definitions(.:format)            admin/task_definitions#index
   edit_admin_task_definition GET    /admin/task_definitions/:id/edit(.:format)   admin/task_definitions#edit
        admin_task_definition GET    /admin/task_definitions/:id(.:format)        admin/task_definitions#show

_path_url结尾的未定义方法错误通常表示路由助手不正确。

检查错误点,似乎有一个助手方法(bootstrap_form_for)正在调用路由助手task_definitions_path,这是不正确的。根据您的路由文件,该路由在admin下以名称命名,因此正确的路由助手是:

admin_task_definitions_path  

我不知道bootstrap_form_for helper方法中有什么,所以我没有特定的修复程序。假设您使用Bootstrap表单gem,请跳过它并手动编写表单。


将来,rake routes将显示所有已注册的路由助手。调试坏路由助手的方便列表。

最新更新