自Rails3升级以来对空params []哈希进行故障排除

时间:2011-01-25 05:54:34

标签: ruby-on-rails-3

我有一个在控制台中正确测试的命名路由并显示:url_title应该包含在params []中,但params []总是为空。

问题是,为什么params []为空?我的期望是它应该有params [:url_title]。

我也删除了这条路线并使用了默认资源,而params []仍然是空的。

我一直在使用记录器检查params。

我的应用程序是从Rails 2.3.5升级到Rails 3.0.3。

以下是正在进行的代码摘要。

# this is my route
match 'papers/:url_title' => 'papers#show', :as => :permalinkpaper

# this is link_to and the generated url being called
<%= link_to paper.title, paper_path(paper.url_title) %>
http://localhost:3000/papers/great-passion

# which properly matches to this controller#action for papers#show 
def show
    @paper = Paper.where(:url_title => params[:url_title]).first()
    PaperHistory.add( current_user, @paper.id )

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @paper }
      format.json { render :json => @paper }
      format.mobile # { render :layout => false }
    end
  end

# which generals this error because the Paper looking returns noting because the params[:url_title] is nil
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

# the log stack trace
Started GET "/papers/great-passion" for 127.0.0.1 at Mon Jan 24 23:04:04 -0600 2011
  Processing by PapersController#show as HTML

  SQL (0.7ms)  SHOW TABLES
  SQL (0.5ms)  SHOW TABLES
  Paper Load (0.7ms)  SELECT `papers`.* FROM `papers` WHERE (`papers`.`url_title` IS NULL) ORDER BY title LIMIT 1
Completed   in 119ms

RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
  app/controllers/papers_controller.rb:43:in `show'



# I've validated the route in the console and it seems to know :url_title is the proper value
>> r = ActionController::Routing::Routes
>> r.recognize_path "/papers/great-passion"
=> {:action=>"show", :url_title=>"great-passion", :controller=>"papers"} 

更新:我发现当值在URL中时,params []不为空,例如在执行搜索时。

http://localhost:3000/papers?utf8=%E2%9C%93&keywords=passion

这成功地产生了

Started GET "/papers?utf8=%E2%9C%93&keywords=passion" for 127.0.0.1 at Tue Jan 25 00:20:07 -0600 2011
  Processing by PapersController#index as HTML
  Parameters: {"utf8"=>"✓", "keywords"=>"passion"}
params: utf8✓keywordspassion

2 个答案:

答案 0 :(得分:2)

感谢大家的帮助。我能够一块一块地拆解我的应用程序,最后让params []显示出来。

罪魁祸首是 open_id_authentication 插件。

我在供应商目录中有一些插件,所以我将它们全部删除了,并且在克服了一些错误后(b / c插件现在丢失了)一切正常。我系统地更换了插件,当我得到open_id_authentication时发现params []再次消失。

答案 1 :(得分:0)

对于这个问题,我有一个不同的解决方案,我将在这里发布,以防有​​人遇到同样的问题。我正在研究网站的密码重置部分并尝试了各种各样的事情,例如手动放置URL,指定控制器和操作,使用裸(最小)形式等,但所有这些都失败了。给出的错误是电子邮件参数为空白。

查看Exceptional日志显示,不仅电子邮件参数为空,而且甚至缺少UTF-8复选标记。 params hash中唯一的东西是控制器和动作。重新加载页面也没有出现关于重新提交信息的常见方法。

事实证明问题出在 SSL 。该页面试图使用SSL,但没有权限,它以某种方式默默地杀死表单。希望能有所帮助。

相关问题