专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Ruby/Rails

redirect_to出错了,该怎么解决

发布时间:2011-06-29 18:42:47 文章来源:www.iduyao.cn 采编人员:星星草
redirect_to出错了
我的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
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: