我的controller
- Java code
class QuestrionController < ApplicationController def show @id = params[:id] @questrons = Questron.find(:all, :conditions =>"dep_id ="+@id.to_s) end def particular end def response end def issuance if params[:id] @id = params[:id] end if request.post? que = Questron.new(params[:questron]) que.que_time = Time.now que.dep_id = params[:id] if que.save redirect_to :action => "show",:id => @id.to_s else redirect_to :controller => "errors", :action => "all" end end end end
调用issuance方法save后redirect_to出错。
页面的信息是:
You have a nil object when you didn't expect it!
The error occured while evaluating nil.redirect
------解决方案--------------------
- Python code
if params[:id] @id = params[:id] end
------解决方案--------------------
- Java code
if params[:id] @id = params[:id] end
------解决方案--------------------
redirect_to :action => "show",:id => @id.to_s
这个位置的to_s有问题。@id应该是一个integer,跳转是会find_by_id, 如果是一个string的id,当然无法查询到,只能返回nil值。
------解决方案--------------------
不好用啊
------解决方案--------------------
没有to_s
------解决方案--------------------
que = Questron.new(params[:questron])
question 不是 questron。你模型都写错了,咋去找对应的表!服你了!
------解决方案--------------------
要坚持rails风格写代码,要空格!
redirect_to :action => "show",:id => @id.to_s
上面的应该这么写!
redirect_to :action => "show", :id => @id
------解决方案--------------------
@id取到的可能是一个nil,所以要保证@id不为空,而且不是to_s,不加试试,或者用to_i
------解决方案--------------------
把你的url贴出来看看,可用中断调试下,估计是redirect_to :action => "show",:id => @id.to_s 中的@id
由
if params[:id]
@id = params[:id]
end
获取的适合,为空,所以会出现这个问题。
URI 是这个样子吗?questriones/id/issuance吗?
------解决方案--------------------
dddddddddddddddddddddddd