首先搞清楚这句话,在 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. hdu 4717 The Moving Points(三分)

    http://acm.hdu.edu.cn/showproblem.php?pid=4717 大致题意:给出每一个点的坐标以及每一个点移动的速度和方向. 问在那一时刻点集中最远的距离在全部时刻的最远距 ...

  2. 解决的方法:mysql_connect()不支持请检查mysql模块是否正确载入

    故障现象:linux 安装discuz 错误提示:mysql_connect() 不支持请检查mysql模块是否正确载入. 解决的方法:查看/usr/lib/php/modules/ (64位的看/u ...

  3. sedna载入xml文件

    如果有一个xml文件a.xml.须要把它载入到sedna数据库xml_db里. sedna是通过se_term把xml载入到数据库的.有两种方法: 1.通过se_term的-query參数. se_t ...

  4. leetCode(32):Power of Two

    Given an integer, write a function to determine if it is a power of two. 2的幂的二进制表示中,必定仅仅有一个"1&q ...

  5. hdu 4997 Biconnected

    这题主要是计算连通子图的个数(c)和不连通子图的个数(dc)还有连通度为1的子图的个数(c1)和连通度为2以上的子图的个数(c2)之间的转化关系 主要思路大概例如以下: 用状态压缩的方法算出状态为x的 ...

  6. Hive权限之改进

    不足 即使开启hive权限认证的情况下,不论什么用户仍然是超级用户.能够通过grant给不论什么人赋予不论什么权限,这样权限认证基本没有意义.因此必须在开启权限认证的同一时候.对运行grant/rev ...

  7. Codeforces Round #FF (Div. 2):B. DZY Loves Strings

    B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. 第6章7节《MonkeyRunner源代码剖析》Monkey原理分析-事件源-事件源概览-注入按键事件实例

    在事件生成并放入到命令队列后,Monkey类的runMonkeyCycles就会去调用相应事件源的getNextEvent来获的事件来运行事件注入,那么这一小节我们通过MonkeyKeyEvent这个 ...

  9. UVA 11000- Bee 递推

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  10. JavaScript——BOM(浏览器对象模型),时间间隔和暂停

    BOM(浏览器对象模型):能够对浏览器的窗体进行訪问和操作 1.主要的BOM体系: window------------document-------------------------------- ...