NameError:未定义的局部变量或方法`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
搭建名为 ClockEntry 的 Rails CRUD,当我点击创建按钮时,我得到
NameError: undefined local variable or method `clock_entry_params' for #
这是下面生成的迁移:
class CreateClockEntries
这是运行迁移后生成的架构:
create_table "clock_entries", force: :cascade do |t|t.bigint "user_id", null: falset.string 目的"t.datetime "time_in"t.datetime "time_out"t.datetime "created_at", 精度: 6, null: falset.datetime "updated_at", 精度: 6, null: falset.index ["目的"],名称:"index_clock_entries_on_purpose"t.index ["time_in"],名称:"index_clock_entries_on_time_in"t.index ["time_out"],名称:"index_clock_entries_on_time_out"t.index ["user_id"],名称:"index_clock_entries_on_user_id"结尾
创建表单:注意我编写了其他设置 time_in
和 time_out
的函数.它们位于控制器内的我的私有方法中.
<%= simple_form_for(@clock_entry) do |f|%><%= f.error_notification %><%= f.error_notification 消息:f.object.errors[:base].to_sentence if f.object.errors[:base].present?%><div class="form-inputs"><%#= f.association :user %><%= f.input :目的%><%#= f.input :time_in %><%#= f.input :time_out %>
<div class="form-actions"><%= f.button :submit %>
<%结束%>
控制器:
class ClockEntriesController <应用控制器定义新@clock_entry = ClockEntry.new结尾定义创建@clock_entry = ClockEntry.new(clock_entry_params)response_to do |格式|如果@clock_entry.saveformat.html { redirect_to @clock_entry,注意:'时钟条目已成功创建.'}format.json { 渲染:显示,状态::创建,位置:@clock_entry }别的format.html { 渲染:新}format.json { 渲染 json: @clock_entry.errors, 状态: :unprocessable_entity }结尾结尾结尾私人的def set_time_in@clock_entry.time_in = Time.now结尾def set_time_out@clock_entry.time_in = Time.now结尾def clock_entry_paramsparams.require(:clock_entry).permit(:user_id, :目的, :time_in, :time_out)结尾结尾
任何帮助将不胜感激.注意:我正在使用 Postgres
现在我想我知道我在哪里以及为什么会出现这个错误.正如我所说,我在设置 time_in
和 time_out
的私有方法中编写了其他函数,这些方法的外观如下:
def set_time_in@clock_entry.time_in = Time.now结尾def set_time_out@clock_entry.time_in = Time.now结尾
首先,业务逻辑功能不应该在控制器内部.要阅读业务逻辑,请查看 https://www.reddit.com/r/rails/comments/77eesr/what_is_business_logic/
我是如何解决这个问题的?:
1.将所有业务逻辑方法移至您的模型.所以现在我有:
class ClockEntry <申请记录归属地:用户验证:目的,存在:真实def set_time_inself.time_in = Time.now结尾def set_time_outself.time_in = Time.now结尾结尾
2.然后你可以从你的控制器调用这些函数.所以我有:
class ClockEntriesController <应用控制器定义创建@clock_entry = current_user.clock_entries.new(clock_entry_params)@clock_entry.set_time_in # 我在这里调用这些逻辑.response_to do |格式|如果@clock_entry.saveformat.html { redirect_to @clock_entry,注意:'时钟条目已成功创建.'}format.json { 渲染:显示,状态::创建,位置:@clock_entry }别的format.html { 渲染:新}format.json { 渲染 json: @clock_entry.errors, 状态: :unprocessable_entity }结尾结尾结尾私人的def set_clock_entry@clock_entry = ClockEntry.find(params[:id])结尾def clock_entry_paramsparams.require(:clock_entry).permit(:user_id, :目的, :time_in, :time_out)结尾结尾
这就是我所做的一切,而且效果很好.我认为有时阅读关于你的堆栈的内容并用你必须忘记的最小的东西来刷新是值得的.我希望这可以帮助别人.Gracias(谢谢)
.
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
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)
.
这篇关于NameError:未定义的局部变量或方法`clock_entry_params' for #<ClockEntriesController:0x00007f9e4347c208>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!