自 Rails3 升级以来对空 params[] 哈希进行故障排除 [英] Troubleshooting empty params[] hash since Rails3 Upgrade
问题描述
我有一个命名路由,可以在控制台中正确测试并显示应该包含在 params[] 中的 :url_title,但 params[] 始终为空.
I have a named route that tests properly in the console and shows the :url_title which should be included in params[], yet params[] is always empty.
问题是,为什么 params[] 是空的?我的期望是它应该有 params[:url_title].
The question is, why is params[] empty? My expectation is it should have params[:url_title].
我也删除了这条路由并使用了默认资源,params[] 仍然是空的.
I also removed this route and used the default resource and params[] is still empty.
我一直在使用记录器检查参数.
I've been checking params by using logger.
我的应用是从 Rails 2.3.5 升级到 Rails 3.0.3.
My app is an upgrade from Rails 2.3.5 to Rails 3.0.3.
这是正在发生的事情的代码摘要.
Here's a code summary of what's going on.
# 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[] 不为空,例如在执行搜索时.
UPDATE: I have found that params[] are NOT empty when values are in the URL, such as when performing a search.
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
推荐答案
感谢大家的帮助.我能够一块一块地反汇编我的应用程序,最后让 params[] 显示出来.
Thanks to all for the help. I was able to disassemble my app piece by piece and finally get params[] to show up.
罪魁祸首是open_id_authentication插件.
我在供应商目录中有一些插件,所以我将它们全部删除,在克服了一些由此产生的错误(b/c 插件现在丢失)后,一切正常.我系统地更换了插件,到了open_id_authentication的时候发现params[]又消失了.
I had some plugins in the vendors directory, so I removed them all and after hurdling a few resulting errors (b/c the plugins were now missing) everything worked. I systematically replaced plugins, and when I got to open_id_authentication found that the params[] again disappeared.
这篇关于自 Rails3 升级以来对空 params[] 哈希进行故障排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!