未定义的局部变量或方法 'f' - Ruby on Rails
Undefined local variable or method 'f' - Ruby on Rails
我正在学习教程。无论我做什么,这个错误仍然不断出现,想知道造成这种情况的原因。这让我抓狂。我相信我使用的是正确的 haml 缩进,因为这不会产生语法错误。是的,我确实安装了 haml、bootstrap 和 simple_form gem。
我有一个名为 pins_controller 的控制器,我在其中定义了以下内容:
def new
@pin = Pin.new
end
def create
@pin = Pin.new(pin_params)
if @pin.save
redirect_to @pin, notice: "Successfully saved new pin"
else
render 'new'
end
end
private
def pin_params
params.require(:pin).permit(:title, :description)
end
这是呈现错误的视图文件的代码:
= simple_form_for @pin, html: {multipart: true} do |f|
- if @pin.errors.any?
#errors
%h2
= pluralize(@pin.errors.count, "error")
prevented this Pin from saving
%ul
- @pin.errors.full_messages.each do |msg|
%li = msg
.form-group
= f.input :title, input_html: { class: 'form-control' }
.form-group
= f.input :description, input_html: { class: 'form-control'}
= f.button :submit, class: "btn btn-primary"
假设 haml 格式正确(有时我在使用 SO 代码时遇到问题),你的问题是你的缩进。 .form-group
以下的所有内容都需要再缩进几个空格,以便结构与第 2 行匹配。
我确定您已经读到 haml 对空格敏感(这对某些人来说很奇怪),但如果您的缩进错误,那么它就不会显示。因此,由于您在最后几行中出现了凹痕,因此您引用了块参数 (f),但它不在范围内。
现在 haml 正在解释您的 do |f|
块在
行结束
- @pin.errors.full_messages.each do |msg|
%li = msg
尝试更改它并查看该错误是否消失。
我正在学习教程。无论我做什么,这个错误仍然不断出现,想知道造成这种情况的原因。这让我抓狂。我相信我使用的是正确的 haml 缩进,因为这不会产生语法错误。是的,我确实安装了 haml、bootstrap 和 simple_form gem。
我有一个名为 pins_controller 的控制器,我在其中定义了以下内容:
def new
@pin = Pin.new
end
def create
@pin = Pin.new(pin_params)
if @pin.save
redirect_to @pin, notice: "Successfully saved new pin"
else
render 'new'
end
end
private
def pin_params
params.require(:pin).permit(:title, :description)
end
这是呈现错误的视图文件的代码:
= simple_form_for @pin, html: {multipart: true} do |f|
- if @pin.errors.any?
#errors
%h2
= pluralize(@pin.errors.count, "error")
prevented this Pin from saving
%ul
- @pin.errors.full_messages.each do |msg|
%li = msg
.form-group
= f.input :title, input_html: { class: 'form-control' }
.form-group
= f.input :description, input_html: { class: 'form-control'}
= f.button :submit, class: "btn btn-primary"
假设 haml 格式正确(有时我在使用 SO 代码时遇到问题),你的问题是你的缩进。 .form-group
以下的所有内容都需要再缩进几个空格,以便结构与第 2 行匹配。
我确定您已经读到 haml 对空格敏感(这对某些人来说很奇怪),但如果您的缩进错误,那么它就不会显示。因此,由于您在最后几行中出现了凹痕,因此您引用了块参数 (f),但它不在范围内。
现在 haml 正在解释您的 do |f|
块在
- @pin.errors.full_messages.each do |msg|
%li = msg
尝试更改它并查看该错误是否消失。