{"..."} 的未定义方法 `model_name'

undefined method `model_name' for {"..."}

我有一个设置表单,但在传递数据和保存数据时遇到了一些问题。

我有以下代码

路线

get 'settings', to: 'settings#index'
put 'settings', to: 'settings#update_all'

控制器

def index
end

def update_all
  params[:setting].keys.each do |index, value|
    setting = Setting.find_by(name: index)
    
    if setting
      setting.value = value
      setting.save
    end
  end
end

private

def setting_params
  params.permit(
    # company details
    :company_name,
    :company_system_display_name,
    :company_address_1,
    :company_address_2,
    :company_address_3,
    :company_address_4,
    :company_postcode,
    :company_default_country_id,
    :company_web_site,
    :company_main_contact,
    :company_main_telephone,
    :company_main_fax,
    :company_main_mail,
    :company_accounts_contact,
    :company_accounts_telephone,
    :company_accounts_fax,
    :company_accounts_mail,
    :delivery_contact_name,
    :setting_delivery_address_1,
    :setting_delivery_address_2,
    :setting_delivery_address_3,
    :setting_delivery_address_4,
    :delivery_postcode,
    :delivery_country_id,

    # tax payroll
    :company_incorporation_status,
    :company_registration_number,
    :number_of_partners,
    :is_vso,
    :company_corporation_tax_number,
    :is_efpm,
    :company_year_end,
    :company_year_end,
    :paye_sender_id,
    :paye_pass,
    :paye_eoy_mail,
    :company_vat_status,
    :company_vat_number,
    :cash_accounting,
    :cash_accounting,
    :vat_adjustment_limit,
    :btp_enabled,
    :btp_api_url,
    :btp_realm,
    :btp_client_id,
    :btp_client_secret,
    :eori_number,
    "Personal Travel",
    "Personal Motoring",
    "Personal Telephone",
    :company_paye_tax_number,
    :rti_start_date,
    :payroll_bacs_sort_code,
    :paye_accounts_office_reference_number,
    :econ,
    :week_end_day,
    :weekly_pay_day,
    :payroll_first_two_week_pay_date,
    :payroll_first_four_week_pay_date,
    :standard_sp_recovery_rate,
    :nic_compensation,
    :hmrc_employers_nic_allowance,

    # system settings
    :bcc_address,
    :check_stock_levels,
    :default_purchase_invoice_price_type,
    :discounts_enabled,
    :enable_custom_documents,
    :doc_layout,
    :nav_type,
    :valid_for,
    :quote_value,
    :default_true_create_supply_orders,
    "exclude_non-manufactured_from_wo_creation_from_so",
    :remove_works_order_from_plan_after_edit,
    :include_sales_order_notes_on_picking_lists,
    :user_change_own_password,
    :auto_populate_receive_invoice,
    :assume_supplier_has_vat_number_for_auto_populate_receive_invoice,
    :backorder_default,
    :consolidate_work_order,
    :over_delivery_default,
    :use_package_lines,
    :hightlight_below_order_quantity_on_grn,
    :show_poi_descriptions_on_order_lines,
    # VAT RATES
    :default_stock_item_vat_rate_id,
    :margin_markup,
    :restrict_purchase_invoice_query_flag,
    :update_probability_on_opportunity_status_change,
    :default_stock_item_vat_rate_id,
    :enable_comments_on_stock_components,
    :no_supplier_serial_numbers,
    :enable_batch_number_generation,
    :intrastat_enable,
    :intrastat_enable_tod,
    :wo_process_start_time,
    :negative_current_stock,
    "default_customer_payment_term_id",
    "default_supplier_payment_term_id",
    :min_supplier_payment,
    :show_vat_on_uninvoiced_sales,
    :hide_due_date_on_sales_quotes_and_orders,
    :hide_due_date_on_purchase_quotes_and_orders,
    :inherit_default_sales_accounts,
    :logo_position,
    :company_logo,
    :company_logo_image_file,
    :company_logo_image_file,
    :kpi_plan_vat_rate,
    :kpi_plan_payroll_rate,
    :kpi_use_average_cos,
    :kpi_use_average_labour,
    :sales_order_item_custom_info_1_form_name,
    :sales_order_item_custom_info_1_enabled,

    # trade terms
    :terms_of_trade,

    # factoring
    :factor_type,
    :factor_user,
    :factor_pass,
    :factoring_image_file,
    :default_repair_order_stock_item_id,
    :default_repair_stock_location_id,
    :default_repair_labour_stock_item_id,

    # document storage
    :document_storage_type,
    :dropbox_access_token,

    # web api
    :api_url,
    :api_key,
    :api_enabled
  )
end

指数

<% content_for :title, "Edit Setting" %>

<div class="l-12col">
    <h1>Edit Settings</h1>
</div>

<%= render partial: "form", locals: { setting: @setting, submit_text: "Update" } %>

形式

<div class="l-12col" id="settings_form">
  <%= form_with model: setting, url: settings_path, method: :put, multipart: true do |f| %>
    <div class="tabs">
      <!-- start tab headings -->
      <div class="tab-headings">
        <a class="tab-button active" data-id="company_details">Company Details</a>
        <a class="tab-button" data-id="tax_payroll">Tax &amp; Payroll</a>
        <a class="tab-button" data-id="system_settings">System Settings</a>
        <a class="tab-button" data-id="trade_terms">Trade Terms</a>
        <a class="tab-button" data-id="factoring">Factoring</a>
        <a class="tab-button" data-id="document_storage">Document Storage</a>

        <% if Features.API? %>
          <a class="tab-button" data-id="web_api">Web API</a>
        <% end %>
      </div>
      <!-- end tab headings -->

      <div class="tab-contents">
        <!-- start of company_details tab -->
        <div class="tab-content active" id="company_details">
          <div class="l-row-block clearfix">
            <div class="l-06col l-ml-12col l-md-12col">
              <h2 class="txt-title-alt">
                Name &amp; Address

                <span data-tooltip title="Full legal name and address for this business as it should appear on business documentation.">
                  <span class="icon-help-with-circle" aria-hidden="true"></span>
                </span>
              </h2>

              <div class='field l-margin-sm'>
                <%= f.label :company_name %>
                <%= f.text_field :company_name %>
              </div>

              <div class='field l-margin-sm checkbox_form'>
                <%= f.check_box :company_incorporation_status %>
                <%= f.label :company_incorporation_status, "Incorporated?" %>
              </div>
            </div>
          </div>
        </div>
        <!-- end of company_details tab -->
      </div>
    </div>

    <div class="btns">
      <%= submit_tag submit_text, id: 'company_settings_update_btn' %>
      <%= link_to "Cancel", root_path, class: "btn-medium" %>
    </div>
  <% end %>
</div>

数据库结构

错误:

NoMethodError at /settings
undefined method `model_name' for {"quote_format"=>"QU-%5n", "quote_value"=>1, "company_name"=>"Your Business Name", "company_address_1"=>"Address Line 1", "company_address_2"=>"Address Line 2", "company_address_3"=>"Address Line 3", "company_address_4"=>"Address Line 4", "company_postcode"=>"Postcode", "company_main_contact"=>"Your Name", "company_main_telephone"=>"Your Telephone Number", "company_main_fax"=>"Your Fax Number", "company_main_mail"=>"Your e-Mail", "company_accounts_contact"=>"", "company_accounts_telephone"=>"", "company_accounts_fax"=>"", "company_accounts_mail"=>"", "company_web_site"=>"Your Web Site", "company_incorporation_status"=>true, "company_registration_number"=>"", "company_corporation_tax_number"=>"", "company_paye_tax_number"=>"", 

这是一个遗留应用程序我刚刚重新构建了表单并试图让它再次工作对我来说数据库结构很奇怪我个人会正确地使用 ActiveRecord::Store 但我们可以不要更改数据库中的任何内容。

所以如果有人有任何想法那就太好了。

编辑:经过上述更改后,我现在有以下内容

NoMethodError at /settings
undefined method `keys' for nil:NilClass

Hint: Something is `nil` when it probably shouldn't be.

编辑 2: 当 运行 rails c 我得到以下

Running via Spring preloader in process 5793
Loading development environment (Rails 6.1.3.2)
[1] pry(main)> @settings
=> nil
[2] pry(main)> @setting
=> nil
[3] pry(main)> Setting.all
  Setting Load (0.4ms)  SELECT `settings`.* FROM `settings`
=> [#<Setting:0x000056318efee410
  id: 1,
  name: "quote_format",
  value: "QU-%5n",
  value_type: "string",
  created_at: Tue, 13 Jul 2021 20:49:16.000000000 BST +01:00,
  updated_at: Tue, 13 Jul 2021 20:49:16.000000000 BST +01:00,
  created_by: nil,
  updated_by: nil>,
 #<Setting:0x000056318f1b2df0
  id: 2,
  name: "quote_value",
  value: "1",
  value_type: "integer",
  created_at: Tue, 13 Jul 2021 20:49:16.000000000 BST +01:00,
  updated_at: Tue, 13 Jul 2021 20:49:16.000000000 BST +01:00,
  created_by: nil,
  updated_by: nil>,
 #<Setting:0x000056318f1b2c10
  id: 3,
....

经过几天对代码的研究,下面是有效的方法。

形式

<%= form_with(model: setting, scope: :settings, method: :post, multipart: true) do |f| %>

控制器

def index
  settings = {}

  Setting.all.each do |setting|
    settings[setting.name] = setting.value
  end

  @settings = OpenStruct.new(settings)
end

def update
  @errors = []
  @error_list = []

  params[:settings].each do |name, value|
    setting = Setting.find_by(name: name)
    next unless setting.present?

    setting.update(value: value)
  end

  redirect_to settings_path, notice: 'Settings have been updated'
end

现在让错误和其他字段正常工作。