0

假设我在 View 中定义了一个表单

module Admin
  module Views
    module Dashboard
      class New
        include Admin::View

        def form
          form_for :link, routes.links_path do
            text_field :url
            submit 'Create'
          end
        end
...

我错过了什么吗?因为下面的例子不起作用:

module Admin
  module Views
    module Dashboard
      class Index
        include Admin::View
        include Dashboard::New
...
4

1 回答 1

2

您不能以这种方式将代码从一个视图共享到另一个视图。您的代码段不起作用,因为 Ruby 不允许将类包含到另一个类中。所以,如果你想这样做 - 你应该使用帮助模块。对于您的情况,它应该如下所示:

module Admin
  module Helpers
    module Dashboard
      def form
        form_for :link, routes.links_path do
          text_field :url
          submit 'Create'
        end
      end
    end
  end
end

并将其包含在您的视图中

module Admin
  module Views
    module Dashboard
      class New
        include Admin::View
        include Admin::Helpers::Dashboard

        # ...
      end
    end
  end
end

或将其全局包含在您的应用中

# apps/admin/application.rb

view.prepare do
  include Hanami::Helpers
  include Admin::Helpers::Dashboard
end

文档:https ://guides.hanamirb.org/helpers/custom-helpers/

于 2018-12-25T10:46:16.730 回答