NameError: undefined local variable or method `clock_entry_params’ for #<ClockEntriesController:0x00007f9e4347c208>

NameError: undefined local variable or method `clock_entry_params’ for #<ClockEntriesController:0x00007f9e4347c208>

我在玩 Rails 6,但我的强参数有问题。我用 rails g scaffold ClockEntry user:references purpose:string time_in:datetime time_out:datetime 构建了 Rails 称为 ClockEntry 的 CRUD,当我点击创建按钮时,我得到

NameError: undefined local variable or method `clock_entry_params’ for #<ClockEntriesController:0x00007f9e4347c208>

下面是生成的迁移:

class CreateClockEntries < ActiveRecord::Migration[6.0]
  def change
    create_table :clock_entries do |t|
      t.references :user, null: false, foreign_key: true
      t.string :purpose
      t.datetime :time_in
      t.datetime :time_out

      t.timestamps
    end
    # add_index :clock_entries, %i[purpose time_in time_out]
    add_index :clock_entries, :purpose
    add_index :clock_entries, :time_in
    add_index :clock_entries, :time_out
  end
end

下面是 运行 迁移后生成的架构:

create_table "clock_entries", force: :cascade do |t|
    t.bigint "user_id", null: false
    t.string "purpose"
    t.datetime "time_in"
    t.datetime "time_out"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["purpose"], name: "index_clock_entries_on_purpose"
    t.index ["time_in"], name: "index_clock_entries_on_time_in"
    t.index ["time_out"], name: "index_clock_entries_on_time_out"
    t.index ["user_id"], name: "index_clock_entries_on_user_id"
  end

创建表单: 注意我写了其他设置 time_intime_out 的函数。它们在我控制器内的私有方法中。

<%= simple_form_for(@clock_entry) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%#= f.association :user %>
    <%= f.input :purpose %>
    <%#= f.input :time_in %>
    <%#= f.input :time_out %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

控制器:

class ClockEntriesController < ApplicationController
  def new
    @clock_entry = ClockEntry.new 
  end

  def create
    @clock_entry = ClockEntry.new(clock_entry_params)

    respond_to do |format|
      if @clock_entry.save
        format.html { redirect_to @clock_entry, notice: 'Clock entry was successfully created.' }
        format.json { render :show, status: :created, location: @clock_entry }
      else
        format.html { render :new }
        format.json { render json: @clock_entry.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def set_time_in
    @clock_entry.time_in = Time.now
  end

  def set_time_out
    @clock_entry.time_in = Time.now
  end

  def clock_entry_params
    params.require(:clock_entry).permit(:user_id, :purpose, :time_in, :time_out)
  end
end

如有任何帮助,我们将不胜感激。注意:我正在使用 Postgres

现在我想我知道在哪里以及为什么会出现此错误。正如我所说,我在设置 time_intime_out 的私有方法中编写了其他函数,这些方法的外观如下:

  def set_time_in
    @clock_entry.time_in = Time.now
  end

  def set_time_out
    @clock_entry.time_in = Time.now
  end

首先,业务逻辑函数不应该在控制器内部。要阅读业务逻辑,请检查 https://www.reddit.com/r/rails/comments/77eesr/what_is_business_logic/

我是怎么解决这个问题的?:

1.将所有业务逻辑方法移至您的模型。所以现在我有:

class ClockEntry < ApplicationRecord
  belongs_to :user

  validates :purpose, presence: true

  def set_time_in
    self.time_in = Time.now
  end

  def set_time_out
    self.time_in = Time.now
  end
end

2。然后你可以从你的控制器调用这些函数。所以我有:

class ClockEntriesController < ApplicationController
  def create
    @clock_entry = current_user.clock_entries.new(clock_entry_params)
    @clock_entry.set_time_in # I am calling those logics here.

    respond_to do |format|
      if @clock_entry.save
        format.html { redirect_to @clock_entry, notice: 'Clock entry was successfully created.' }
        format.json { render :show, status: :created, location: @clock_entry }
      else
        format.html { render :new }
        format.json { render json: @clock_entry.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def set_clock_entry
    @clock_entry = ClockEntry.find(params[:id])
  end

  def clock_entry_params
    params.require(:clock_entry).permit(:user_id, :purpose, :time_in, :time_out)
  end

end

这就是我所做的一切,而且效果很好。我认为有时候阅读有关您的堆栈的内容并用您一定已经忘记的最小的事情来刷新是值得的。我希望这可以帮助别人。 Gracias (Thanks).