ruby on rails错误undefined method `title' for nil:NilClass

首先搞清楚这句话,在 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' for nil:NilClass

时间: 2024-08-03 17:16:03

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

ruby on rails错误undefined method `title&#39; 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_t

undefined method `environment&#39; for nil:NilClass when importing Bootstrap into rails

今天做项目时往Gemfile里加了各gem, 然后bundle update了一下, 然后悲剧了,出现了undefined method `environment' for nil:NilClass when importing Bootstrap into rails错误, 各种不理解. 然后查了一下, 找到了解决方案: https://stackoverflow.com/questions/22392862/undefined-method-environment-for-nilnilcla

&quot;undefined method `root&#39; for nil:NilClass&quot; error when using &quot;pod install&quot; 解决办法

如果pod undate 的时候报错"undefined method `root' for nil:NilClass" error when using "pod install" 或 NoMethodError - undefined method `dirname' for nil:NilClass 检查你的podfile文件 保证一下信息存在 Pod::Spec.new do |s| s.name = "DDEBusiness" s.ve

ruby on rails 的使用过程中,遇到的错误及解决办法

1./data/drolay11/lcb_saas/tmp/build-147274463612855/vendor/bundle/ruby/2.1.0/bin/rake: symbol lookup error: /data/drolay11/lcb_saas/tmp/build-147274463612855/vendor/bundle/ruby/2.1.0/gems/mysql2-0.3.17/lib/mysql2/mysql2.so: undefined symbol: mysql_ge

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

在文章 Ruby On Rails中REST API使用示例--基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求访问IBM Bluemix上的语言翻译服务时,提示:end of file reached 代码如下: auth = "c9819718-4660-441c-9df7-07398950ea44:qUvrJPqwsgOx"; surl = "https://" + auth + "@gateway.wa

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

在文章 Ruby On Rails中REST API使用演示样例--基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求訪问IBM Bluemix上的source=en&target=es&text=hello" style="color: rgb(51, 102, 153); text-decoration: none; font-family: Arial; font-size: 14px; line-height: 26p

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

在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views/orders/xshow的代码 def xcreate flash.now[:notice] = "bb" @order = Order.new(params[:order]) if @order.save flash[:notice] = "yes" render(

[Ruby on Rails系列]4、专题:Rails应用的国际化[i18n]

1. 什么是internationalization(i18n)? 国际化,英文简称i18n,按照维基百科的定义:国际化是指在设计软件,将软件与特定语言及地区脱钩的过程.当软件被移植到不同的语言及地区时,软件本身不用做内部工程上的改变或修正. 当然,官方的定义总是十分高大上.实际上,今天我们要做的并没有那么复杂.任务是为上次的系列3里的Web站点提供多语言支持.比如上次是英语网站,这次我们可以根据用户地点自动提供中文网站或其他语言网站. 2.如何实现i18n? 2.1 Step1: 登录Clou

2--Windows下: RubyMine + Ruby On Rails + mysql 搭建开发环境

最近在接手一个手机项目.在搭建环境的过程中,遇到了一些问题,在下文中已做记录,并奉上个人的解决方案. 开发环境 win2003 ;  JetBrains RubyMine6.3.3 1.  下载最新版ruby,(rubyinstaller-2.0.0-p598.exe ,最新版) 官网:http://rubyinstaller.org/downloads/ 2.  安装ruby 双击安装,安装过程出现如下界面.如图 这里我们选择安装路径为 D:\Ruby200. 下面有3个选项分别是:(1) 是