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

青春壹個敷衍的年華 提交于 2019-12-25 00:48:05

问题


Am playing around with Rails 6 and I have a problem with my strong params. I scaffolded Rails CRUD called ClockEntry with rails g scaffold ClockEntry user:references purpose:string time_in:datetime time_out:datetime and when I hit Create Button I get

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

Here is the migration generated below:

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

Here is the schema is generated below after running migration:

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

The create form: Note I wrote other functions that set time_in and time_out. They are in my private methods inside the controller.

<%= 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 %>

Controller:

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

Any help will be appreciated. Note: Am using Postgres


回答1:


Now I think I know where and why I got this error. As I said I wrote other functions in my private methods that set time_in and time_out and here is how those methods look:

  def set_time_in
    @clock_entry.time_in = Time.now
  end

  def set_time_out
    @clock_entry.time_in = Time.now
  end

First of all, Business Logic functions are not supposed to be inside Controller. To readup on Business Logic, check https://www.reddit.com/r/rails/comments/77eesr/what_is_business_logic/

How did I solve this problem?:

1. Moved all Business Logic methods to your model. So now I have:

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. Then you can call those functions from your controller. So I have:

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

So that is all I have done and it works fine. I think sometimes it pays off to read content about your stack and refreshing with the smallest things you must have forgotten. I hope this helps someone. Gracias (Thanks).



来源:https://stackoverflow.com/questions/58044609/nameerror-undefined-local-variable-or-method-clock-entry-params-for-clocken

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!