ruby on rails错误undefined method `title' for nil:NilClass
首先搞清楚这句话,在 Ruby 中,方法分为
public
、private
和 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' for nil:NilClass的更多相关文章
- 开发新手最容易犯的50个 Ruby on Rails 错误(1)
[编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...
- 11月28日 记录一个错误❌,看ruby on rails --active support core extensions--present? && presence && duplicable?
❌错误 1. @job.resume.count: 提示❌ undefined method `resume' ✅: @job.resumes.count //解释:调出某一个job的所有简历, ...
- undefined method `environment' for nil:NilClass when importing Bootstrap into rails
今天做项目时往Gemfile里加了各gem, 然后bundle update了一下, 然后悲剧了,出现了undefined method `environment' for nil:NilClass ...
- 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 ...
- ruby on rails模拟HTTP请求错误发生:end of file reached
在文章 Ruby On Rails中REST API使用演示样例--基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求訪问IBM Bluemix上的sour ...
- rails undefined method error_messages
rails undefined method error_messages 学习了:http://stackoverflow.com/questions/10002140/use-error-mess ...
- 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 ...
- rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each' for #...
在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views ...
- Git Push问题remote: hooks/update:10 undefined method 'require_relative' for main:Object(NomethodError)
今天在提交代码时遇到到了一个非常蛋疼的问题,remote: hooks/update:10 undefined method 'require_relative' for main:Object(No ...
随机推荐
- php常量,提前定义常量,魔术常量及基本数据类型
常量 定义: 形式一 // define("PI",常量值); define("PI",3.14); define("school",&qu ...
- js+ canvas 实现人物走动
在网上看了一篇管道工玛利亚走动的图片,感觉人物走动的太生涩了,就写了一下代码改动一下: js 代码: //定义数组图片集合 var marios = new Array("image/QQ截 ...
- 在linux環境下安裝jprofiler_linux_8_0_2.sh
1.安装jprofiler_linux_8_0_2.sh chmod+x jprofiler_linux_8_0_2.sh ./jprofiler_linux_8_0_2.sh –c 安装过程略..差 ...
- Mina airQQ聊天 client篇(三)
开发工具 (FlashBuilder4.7) 程序类型(Adobe Air) Flex Air做的桌面程序,效果还挺好看的.最主要是Socket这一块,它也是异步的,而且在Flex中的事件机制比較强大 ...
- Unreal Engine 4 C++ 为编辑器中Actor创建自己定义图标
有时候我们创建场景的时候,特定的Actor我们想给它一个特定的图标,便于观察.比方这样: 实现起来也非常easy.须要编写C++代码: 我们创建一个Actor,叫AMyActor.它包括一个Sprit ...
- 杂项-软件: VBA(Visual Basic for Applications)
ylbtech-杂项-软件: VBA(Visual Basic for Applications) VBA (Visual Basic宏语言) Visual Basic for Application ...
- Java-API-POI:POI百科
ylbtech-Java-API:POI百科 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1. ...
- POJ 3764 DFS+trie树
题意: 给你一棵树,求树中最长的xor路径.(n<=100000) 思路: 首先我们知道 A xor B =(A xor C) xor (B xor C) 我们可以随便选一个点DFS 顺便做出与 ...
- 手游服务器端接入facebook的SDK
手游如果支持facebook登录,就要接入facebook的登录SDK.刚好工作中自己做了这一块的接入功能现在记录分享下来提供一个参考. 当前只是接入了登录这个功能,先简单的说说接入facebook登 ...
- 04《UML大战需求分析》之四
在学习完顺序图之后,流程分析的三种图,我已经学习完了我,但是我还需要大量地锻炼,这样才可以更加熟练地掌握几种图的使用和方法.接下来,我学习了用例图,用来描述系统的行为. 虽然是一同学习的,但是对用例图 ...