首先搞清楚这句话,在 Ruby 中,方法分为
publicprivate
protected
三种,仅仅有 public
方法才干作为控制器的动作。

我的出错的代码例如以下:

controlle

class ArticlesController < ApplicationController
def new
end def create
params.permit!
@article = Article.new(params[:article]) @article.save
redirect_to @article
end private
def article_params
params.required(:article).permit(:title, :text)
end def show
@article = Article.find(params[:id])
end
end

view show.html.erb

<p>
<strong>Title:</strong>
<%= @article.title %>
</p> <p>
<strong>Text;</strong>
<%= @article.text %>
</p>

报错说是:

NoMethodError in Articles#show

Showing /home/huihui/Documents/ruby/blog/app/views/articles/show.html.erb where line #3 raised:

undefined method `title' for nil:NilClass

解决方法是:

在controlle文件中:

class ArticlesController < ApplicationController
def new
end def create
params.permit!
@article = Article.new(params[:article]) @article.save
redirect_to @article
end def show
@article = Article.find(params[:id])
end private
def article_params
params.required(:article).permit(:title, :text)
end
end

把show方法改成公有的就好啦

ruby on rails错误undefined method `title&#39; for nil:NilClass的更多相关文章

  1. 开发新手最容易犯的50个 Ruby on Rails 错误(1)

    [编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...

  2. 11月28日 记录一个错误❌,看ruby on rails --active support core extensions--present? && presence && duplicable?

    ❌错误 1. @job.resume.count: 提示❌   undefined method `resume' ✅: @job.resumes.count  //解释:调出某一个job的所有简历, ...

  3. undefined method `environment' for nil:NilClass when importing Bootstrap into rails

    今天做项目时往Gemfile里加了各gem, 然后bundle update了一下, 然后悲剧了,出现了undefined method `environment' for nil:NilClass ...

  4. Use “error_messages” in Rails 3.2? (raises “undefined method” error)

    I am getting the following error in my Rails 3.2 functional tests: ActionView::Template::Error: unde ...

  5. ruby on rails模拟HTTP请求错误发生:end of file reached

    在文章 Ruby On Rails中REST API使用演示样例--基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求訪问IBM Bluemix上的sour ...

  6. rails undefined method error_messages

    rails undefined method error_messages 学习了:http://stackoverflow.com/questions/10002140/use-error-mess ...

  7. gitlab ActionView::Template::Error (undefined method `[]' for nil:NilClass): 500错误

    Started GET "/mygroup/myproject/tree/master/MyDirectory" for 127.0.0.1 at 2014-10-22 22:42 ...

  8. rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each' for #...

    在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views ...

  9. Git Push问题remote: hooks/update:10 undefined method &#39;require_relative&#39; for main:Object(NomethodError)

    今天在提交代码时遇到到了一个非常蛋疼的问题,remote: hooks/update:10 undefined method 'require_relative' for main:Object(No ...

随机推荐

  1. 本地自旋锁与信号量/多服务台自旋队列-spin wait风格的信号量

    周日傍晚,我去家附近的超市(...)买苏打水,准备自制青柠苏打.我感觉我做的比买的那个巴黎水要更爽口.由于天气太热,非常多人都去超市避暑去了,超市也不撵人,这仿佛是他们的策略.人过来避暑了,走的时候难 ...

  2. UESTC--1271--Search gold(贪心)

    Search gold Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Sta ...

  3. 什么是BOM头(字节顺序标记(ByteOrderMark))

    在utf-8编码文件中BOM在文件头部,占用三个字节,用来标示该文件属于utf-8编码,现在已经有很多软件识别bom头,但是还有些不能识别bom头,比如PHP就不能识别bom头,这也是用记事本编辑ut ...

  4. centos + nodejs + egg2.x 开发微信分享功能

    本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd

  5. NLog日志记录

    配置NLog         NLog支持 .Net 4.5 以及以上版本!              首先去下载NLog的DLL下载地址:http://nlog-project.org/downlo ...

  6. vue-cli安装步骤

    vue-cli脚手架模板是基于node下的npm来完成安装的所以首先需要安装node 条件:  node在4.以上,npm在3以上 安装 指令: 1.npm install -g vue-cli 在全 ...

  7. js获取浏览器缩放比例

    前几天在做项目的时候遇到浏览器缩放比例不为100%时,出来的页面不正常,于是找到了方法获取其比例来通知用户 function detectZoom (){ , screen = window.scre ...

  8. MFC+OpenGL可编程管线

    [github链接] 网上的代码大都是固定管线渲染的,今天下午整理了下,把setPixelFormat.初始化glew.创建GL 4,2 context等操作封装到一个MFC类OpenGLWidget ...

  9. UTC时间和各个地区的时间到底是怎么回事

    就不分析了,直接写结论 1.同一个时间点全球各地的时间戳是一致的 2.同一个时间戳在不同的时区对应不同的时间   依北京时间为例: 当前时间为 Tue Jan 23 2018 19:02:11 GMT ...

  10. jq——css类

    1  addClass(classname) 添加类 <script type="text/javascript"> $("input").clic ...