【问题标题】:Rails (One Month Rails) NoMethodError in PinsController#createRails (一个月 Rails) PinsController#create 中的 NoMethodError
【发布时间】:2013-08-23 19:09:25
【问题描述】:

感谢@skipchris (https://twitter.com/skipchris) 解决了问题。使用调试器我们发现了一个拼写错误。错误报告没有多大意义,但我在调查问题时学到了很多东西!

我昨晚和今天早上都在努力寻找解决方案,但我是 Rails 新手。从我到目前为止所阅读的内容来看,它认为问题与关联有关。我创建了一个没有用户的 Pin(关联错误?),因此路由出现错误,即与设计有关?

当我尝试创建新 Pin 图时,我在本地服务器上的浏览器中收到此错误-

PinsController#create 中的 NoMethodError

pp/controllers/pins_controller.rb:48:in block in create' app/controllers/pins_controller.rb:47:increate'

pins_controller.rb

定义创建

@pin = current_user.pins.new(params[:pin])

respond_to do |format|
  if @pin.save
    format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
    format.json { render json: @pin, status: :created, location: @pin }
  else
    format.html { render action: "new" }
    format.json { render json: @pin.errors, status: :unprocessable_entity }
  end
end   

结束

用户模型(user.rb)

类用户 <:base>

设计:database_authenticable, :registerable, #:recoverable, :rememberable, :trackable, :validatable

attr_accessible :email, :password, :password_confirmation, :remember_me, :name

has_many :pins

结束

【问题讨论】:

    标签: methods devise ruby-on-rails-3.2 rubygems associations


    【解决方案1】:

    是的,我肯定会创建一个用户模型,因为您正在尝试访问方法中的 current_user.pins。你也有吗

    resources :pins
    

    或您的路线中有类似的东西?

    【讨论】:

    • 嗨,布兰登。 defs 就像你写的那样写。从文本编辑器复制并粘贴到 Stackoverflow 必须更改了格式。
    • 所以你没有用户模型?我可能会做其中之一,即使它很简单......因为你正在尝试 current_user.pins
    • 我在模型中有一个 user.rb。我将其添加到问题中
    • 也获得了“resources :pins”
    • 感谢 Brandon 的帮助,但感谢@skipchris (twitter.com/skipchris) 解决了问题。使用调试器我们发现了一个拼写错误。错误报告没有多大意义,但我在调查问题时学到了很多东西!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多