首先搞清楚这句话,在 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. NHibernate之旅(14):探索NHibernate中使用视图

    本节内容 引入 1.持久化类 2.映射文件 3.測试 结语 引入 在数据库操作中,我们除了对表操作,还有视图.存储过程等操作,这一篇和下篇来学习这些内容.这篇我们来学习怎样在NHibernate中使用 ...

  2. itunes connect上传截图提示无法加载文件问题

    解决的方法: 图片名字中不能包括汉字,要英文字母或数字.

  3. 【Java集合源代码剖析】LinkedList源代码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35787253 您好.我正在參加CSDN博文大赛.假设您喜欢我的文章,希望您能帮我投一票,谢 ...

  4. hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)

    Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. USACO 2.2 Preface Numbering

    Preface Numbering A certain book's prefaces are numbered in upper case Roman numerals. Traditional R ...

  6. php global GLOBALS[]变量

    $a=123; function aa() { Global $a; //如果不把$a定义为global变量,函数体内是不能访问函数体外部的$a的,但是可以定义一个相同的名字$a,此时这个变量是局部变 ...

  7. HIT Software Construction Lab 2

    2019年春季学期 计算机学院<软件构造>课程 Lab 2实验报告 姓名 刘帅 学号 班号 1703008 电子邮件 1609192321@qq.com 手机号码 目录   1 实验目标概 ...

  8. 广播Intent的三种方式总结

    1.android有序广播和无序广播的区别 BroadcastReceiver所对应的广播分两类:普通广播和有序广播. 普通广播通过Context.sendBroadcast()方法来发送.它是完全异 ...

  9. CSDN日报20170527 ——《人机大战,历史的见证》

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  10. 微信小程序 | 小程序的转发问题

    1.配置小程序页面静态转发信息 关于小程序转发问题,文档 在 page 页面填加了该监听函数,会在小程序右上角 ... 菜单中显示“转发”按钮: 监听函数需要 return {} 其中的内容配置转发信 ...