Rails admin NoMethodError:未定义的方法`permit!'为零:出口时为NilClass

时间:2018-01-02 18:11:03

标签: ruby-on-rails ruby export rails-admin

虽然我试图在rails admin中导出cvs文件但我遇到了这个错误:

NoMethodError: undefined method `permit!' for nil:NilClass
from /var/lib/gems/2.3.0/gems/rails_admin-1.1.1/lib/rails_admin/config/actions/index.rb:74:in `block (4 levels) in <class:Index>'
from /var/lib/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/mime_responds.rb:201:in `respond_to'
from /var/lib/gems/2.3.0/gems/rails_admin-1.1.1/lib/rails_admin/config/actions/index.rb:42:in `block (2 levels) in <class:Index>'
from /var/lib/gems/2.3.0/gems/rails_admin-1.1.1/app/controllers/rails_admin/main_controller.rb:22:in `instance_eval'
from /var/lib/gems/2.3.0/gems/rails_admin-1.1.1/app/controllers/rails_admin/main_controller.rb:22:in `index'
from /var/lib/gems/2.3.0/gems/rails_admin-1.1.1/lib/rails_admin/config/actions/export.rb:21:in `block (2 levels) in <class:Export>'
from /var/lib/gems/2.3.0/gems/rails_admin-1.1.1/app/controllers/rails_admin/main_controller.rb:22:in `instance_eval...

这是我以前很好的代码!

RailsAdmin.config do |config|

  ### Popular gems integration

  ## == Devise ==
  # config.authenticate_with do
  #   warden.authenticate! scope: :user
  # end
  # config.current_user_method(&:current_user)

  ## == Cancan ==
  # config.authorize_with :cancan

  ## == Pundit ==
  # config.authorize_with :pundit

  ## == Gravatar integration ==
  ## To disable Gravatar integration in Navigation Bar set to false
  # config.show_gravatar true

  config.authorize_with do
    redirect_to main_app.root_path unless current_user.try(:is_admin)
  end

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end
end

在我认为我没有改变任何事情之前没事。这是因为rails admin版本?

1 个答案:

答案 0 :(得分:0)

app / views / rails_admin / main / export.html.haml 中,我对CSV选项进行了评论,以便隐藏选项,如下所示:

    - params = request.params.except(:action, :controller, :utf8, :page, :per_page, :format, :authenticity_token)
- visible_fields = @model_config.export.with(view: self, object: @abstract_model.model.new, controller: self.controller).visible_fields

= form_tag export_path(params.merge(all: true)), method: 'post', class: 'form-horizontal' do

  %input{name: "send_data", type: "hidden", value: "true"}/
  %fieldset{id: 'fields_to_export'}
    %legend
      %i.icon-chevron-down
      = t('admin.export.select')
    .form-group.control-group
      .col-sm-12
        .checkbox
          %label{for: 'check_all'}
            = check_box_tag 'all', 'all', true, { id: 'check_all' }
            %b= t('admin.export.select_all_fields')
    .form-group.control-group
      .col-sm-12
        .well.well-sm{rel: 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), onclick: 'jQuery(this).closest(".control-group").find(".controls").find("input").click()', style: 'margin: 0; cursor: pointer;'}
          %b= t('admin.export.fields_from', name: @model_config.label_plural.downcase)
        .controls
          .row
            - visible_fields.select{ |f| !f.association? || f.association.polymorphic? }.each do |field|
              - list = field.virtual? ? 'methods' : 'only'
              .checkbox.col-sm-3
                - if field.association? && field.association.polymorphic?
                  %label{for: "schema_#{list}_#{field.method_name}"}
                    = check_box_tag "schema[#{list}][]", field.method_name, true, { id: "schema_#{list}_#{field.method_name}" }
                    = field.label + " [id]"
                  - polymorphic_type_column_name = @abstract_model.properties.detect {|p| field.association.foreign_type == p.name }.name
                  %label{for: "schema_#{list}_#{polymorphic_type_column_name}"}
                    = check_box_tag "schema[#{list}][]", polymorphic_type_column_name, true, { id: "schema_#{list}_#{polymorphic_type_column_name}" }
                    = capitalize_first_letter(field.label) + " [type]"
                - else
                  %label{for: "schema_#{list}_#{field.name}"}
                    = check_box_tag "schema[#{list}][]", field.name, true, { id: "schema_#{list}_#{field.name}" }
                    = capitalize_first_letter(field.label)

    - visible_fields.select{ |f| f.association? && !f.association.polymorphic? }.each do |field|
      - fields = field.associated_model_config.export.with(controller: self.controller, view: self, object: (associated_model = field.associated_model_config.abstract_model.model).new).visible_fields.select{ |f| !f.association? }
      .form-group.control-group
        .col-sm-12
          .well.well-sm{rel: 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), onclick: 'jQuery(this).closest(".control-group").find(".controls").find("input").click()', style: 'margin: 0; cursor: pointer;'}
            %b= t('admin.export.fields_from_associated', name: field.label.downcase)
          .controls
            .row
              - fields.each do |associated_model_field|
                - list = associated_model_field.virtual? ? 'methods' : 'only'
                .checkbox.col-sm-3
                  %label{for: "schema_include_#{field.name}_#{list}_#{associated_model_field.name}"}
                    = check_box_tag "schema[include][#{field.name}][#{list}][]", associated_model_field.name, true, { id: "schema_include_#{field.name}_#{list}_#{associated_model_field.name}" }
                    = capitalize_first_letter(associated_model_field.label)

  %fieldset
    %legend
      %i.icon-chevron-down
      = t('admin.export.options_for', name: 'csv')
    / .form-group.control-group
    /   - guessed_encoding = @abstract_model.encoding
    /   %label.col-sm-2.control-label{for: "csv_options_encoding_to"}= t('admin.export.csv.encoding_to')
    /   .col-sm-10.controls
    /     -# from http://books.google.com/support/partner/bin/answer.py?answer=30990 :
    /     = select_tag 'csv_options[encoding_to]', options_for_select(Encoding.name_list.sort), include_blank: true, placeholder: t('admin.misc.search'), :'data-enumeration' => true
    /     %p.help-block= t('admin.export.csv.encoding_to_help', name: guessed_encoding)

    / .form-group.control-group
    /   %label.col-sm-2.control-label{for: "csv_options_skip_header"}= t('admin.export.csv.skip_header')
    /   .col-sm-10.controls
    /     .checkbox
    /       %label{style: 'display: block;' }
    /         = check_box_tag 'csv_options[skip_header]', 'true'
    /     %p.help-block= t('admin.export.csv.skip_header_help')

    / .form-group.control-group
    /   %label.col-sm-2.control-label{for: "csv_options_generator_col_sep"}= t('admin.export.csv.col_sep')
    /   .col-sm-10.controls
    /     = select_tag 'csv_options[generator][col_sep]', options_for_select({ '' => t('admin.export.csv.default_col_sep'), "<comma> ','" => ',', "<semicolon> ';'" => ';', '<tabs>' => "'\t'" }), placeholder: t('admin.misc.search'), :'data-enumeration' => true
    /     %p.help-block= t('admin.export.csv.col_sep_help', value: t('admin.export.csv.default_col_sep'))

  .form-group.form-actions
    .col-sm-offset-2.col-sm-10
      %input{type: :hidden, name: 'return_to', value: (params[:return_to].presence || request.referer)}
      %button.btn.btn-primary{type: "submit", name: 'csv'}
        %i.icon-white.icon-ok
        = t("admin.export.confirmation", name: 'csv')
      / %button.btn.btn-info{type: "submit", name: 'json'}
      /   = t("admin.export.confirmation", name: 'json')
      / %button.btn.btn-info{type: "submit", name: 'xml'}
      /   = t("admin.export.confirmation", name: 'xml')
      %button.btn{type: "submit", name: "_continue"}
        %i.icon-remove
        = t("admin.form.cancel")

但我不评论分隔符选项,所以我取消注释并解决了问题。

.form-group.control-group
  %label.col-sm-2.control-label{for: "csv_options_generator_col_sep"}= t('admin.export.csv.col_sep')
  .col-sm-10.controls
    = select_tag 'csv_options[generator][col_sep]', options_for_select({ '' => t('admin.export.csv.default_col_sep'), "<comma> ','" => ',', "<semicolon> ';'" => ';', '<tabs>' => "'\t'" }), placeholder: t('admin.misc.search'), :'data-enumeration' => true
    %p.help-block= t('admin.export.csv.col_sep_help', value: t('admin.export.csv.default_col_sep'))