NameError:#<ClockEntriesController:0x00007f9e4347c208>的未定义局部变量或方法`clock_entry_params'

时间:2019-09-21 22:32:49

标签: ruby-on-rails ruby-on-rails-6

我正在玩Rails 6,我的强参数有问题。我用rails g scaffold ClockEntry user:references purpose:string time_in:datetime time_out:datetime架起了名为ClockEntry的Rails 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

1 个答案:

答案 0 :(得分:0)

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

time_out

首先,不应将业务逻辑功能包含在Controller中。要阅读Business Logic,请选中https://www.reddit.com/r/rails/comments/77eesr/what_is_business_logic/

我如何解决此问题?:

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

  def set_time_in
    @clock_entry.time_in = Time.now
  end

  def set_time_out
    @clock_entry.time_in = Time.now
  end

2。然后,您可以从控制器中调用这些功能。所以我有:

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

这就是我所做的全部,并且工作正常。我认为有时候阅读您的堆栈内容并用您必须忘记的最小事物来刷新是值得的。我希望这可以帮助别人。 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