0

这是我的用例:

  1. 我有一个使用 Devise for AuthN 的用户模型,我正在使用 Pundit 进行 AuthZ。
  2. 我通过子域约束限制对主应用程序的访问。
  3. 我有一些面向最终用户的页面(在某些时候将是不同的 LandF),我有管理页面等。常见的故事你知道演习。
  4. 我正在使用一个 has_and_belongs_to_many,它使用一个带有 :id 的连接表
  5. 我将控制器、视图目录和迁移命名为复数形式,将模型命名为单数形式。示例:TaskDefinition => 模型、TaskDefinitions => 控制器和表。
  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
4

1 回答 1

0

未定义的方法错误以_path_url通常指示错误的路由助手结尾。

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

admin_task_definitions_path  

我不知道bootstrap_form_for辅助方法中有什么,所以我没有具体的解决方法。假设您使用 Bootstrap 表单 gem,请跳过它并手动编写表单。


将来,rake routes将显示所有已注册的路线助手。用于调试不良路由助手的便捷列表。

于 2014-10-12T19:29:47.073 回答