Ruby on rails 为什么create pins停止工作?

Ruby on rails 为什么create pins停止工作?,ruby-on-rails,Ruby On Rails,所以我刚刚完成了一个月的Rails,在过去两个视频中的某个地方,我在登录时无法创建新的PIN 当用户登录并尝试添加新pin时,会发生此问题 以下是我得到的错误: NoMethodError in PinsController#create undefined method `name' for #<Pin:0x41e0c08> Extracted source (around line #22): def create @pin = current_user.pins.buil

所以我刚刚完成了一个月的Rails,在过去两个视频中的某个地方,我在登录时无法创建新的PIN

当用户登录并尝试添加新pin时,会发生此问题

以下是我得到的错误:

NoMethodError in PinsController#create
undefined method `name' for #<Pin:0x41e0c08>
Extracted source (around line #22):   
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
  redirect_to @pin, notice: 'Pin was successfully created.'
else
  render action: 'new'
pin.rb

validates :image, presence: true
validates :description, presence: true
这似乎消除了第一条错误消息,但现在我看到了一条新消息:

NameError in Pins#show
undefined local variable or method `pin' for #<#<Class:0x3e41920>:0x4e1bbe8>
引脚中的
名称错误#显示
未定义的局部变量或方法“pin”#
来自show.html.erb

<% if current_user && pin.user == current_user %>
<%= link_to edit_pin_path(@pin) do %>
<span class="glyphicon glyphicon-pencil"></span>
Edit
<% end %>
<% end %>

编辑

(再次)帮助?

看起来您正在访问
Pin
模型中某个位置的
name
字段,但是
name
字段既不存在于
pins
表中,也不存在于model
Pin
中的虚拟属性中。这将导致错误
未定义#

的方法“name”,如果查看db/schema.rb,您将看到那里没有name字段

但如果你看app/models/pin.rb

class Pin < ActiveRecord::Base
     belongs_to :user
     has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }


     validates :image, presence: true
     validates :description, presence: true
     validates :name, length: { minimum: 2 }
end
class Pin{:medium=>“300x300>”,:thumb=>“100x100>”}
验证:映像,状态:true
验证:描述,状态:true
验证:名称,长度:{最小值:2}
结束

您将看到一个validates:name,您可能错误地将其添加到该文件中,而不是user.rb。无法验证不存在的字段。

是否删除了名为
name
的列?此处无法链接到整个Github repo以获取调试帮助,也无法链接到实时站点。您的问题需要包含再现问题的最小完整示例。现在您已编辑,这是一个不同的问题。这很好,但是剥夺了那些为现有答案付出时间和努力的人解决问题的机会。如果他们解决了你的第一个问题,请标记正确的答案并开始一个新问题。(你的新问题是你在寻找
pin.user
,而你应该在
@pin.user
之后)
NameError in Pins#show
undefined local variable or method `pin' for #<#<Class:0x3e41920>:0x4e1bbe8>
<% if current_user && pin.user == current_user %>
<%= link_to edit_pin_path(@pin) do %>
<span class="glyphicon glyphicon-pencil"></span>
Edit
<% end %>
<% end %>
class Pin < ActiveRecord::Base
     belongs_to :user
     has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }


     validates :image, presence: true
     validates :description, presence: true
     validates :name, length: { minimum: 2 }
end