帖子中没有方法错误#new [英] NoMethodError in Posts#new

查看:30
本文介绍了帖子中没有方法错误#new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做 Rails 入门教程,当我从shell 我明白了:

I'm doing the Getting started with Rails tutorial and when I run the local server from shell I get this:

`NoMethodError in Posts#new` `/_form.html.erb where line #1 raised:
`undefined method `model_name' for NilClass:Class

这是提取的源代码(围绕第 1 行):

That is the extracted source (around line #1):

1: <%= form_for @post do |f| %>
2:  <% if @post.errors.any? %>
3:  <div id="errorExplanation">
4:      <h2><%= pluralize(@post.errors.count, "error") %> prohibited

我刚刚开始使用 Ruby on Rails,我无法弄清楚发生了什么.我做错了什么?

I just started on Ruby on Rails and I can't figure out what is happening. What am I doing wrong?

推荐答案

您看到的错误消息意味着您有一些包含 nil 对象而不是您期望的实际对象的变量.

The error message, you are seeing means that you have some variable that contains a nil object instead of the actual object you expect.

虽然错误消息没有明确引用此内容,但您的 @post 变量可能为零.

While the error message doesn't explicitly reference this, it is likely your @post variable is nil.

为什么是零?鉴于这里的代码,这几乎是不可能的.请同时发布您的 PostsController#new 操作.

Why is it nil? That's near impossible to say given the code here. Please post your PostsController#new action as well.

这篇关于帖子中没有方法错误#new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆