Rails 路由错误

Posted

技术标签:

【中文标题】Rails 路由错误【英文标题】:Rails Routing Error 【发布时间】:2010-12-26 15:39:50 【问题描述】:

奇怪的错误。我是 Rails 的新手。我从新安装的 rails 连接到一个 oracle 数据库,然后运行:

jruby script/generate scaffold job oid:integer userid:integer name:string status:integer

没有做任何其他事情,我启动了服务器并输入了一个新工作,然后我得到了这个错误:

Routing Error

job_url failed to generate from :controller=>"jobs", :action=>"show", :id=>#<Job id: #<BigDecimal:d55a0f,'10000.0',1(8)>, oid: #<BigDecimal:10bb83e,'1324.0',4(8)>, userid: #<BigDecimal:6d234c,'1234.0',4(8)>, name: "asdfadsf", status: #<BigDecimal:1286c71,'1234.0',4(8)>, created_at: "2009-12-15 00:49:37", updated_at: "2009-12-15 00:49:37">, expected: :controller=>"jobs", :action=>"show", diff: :id=>#<Job id: #<BigDecimal:853e51,'10000.0',1(8)>, oid: #<BigDecimal:1be4050,'1324.0',4(8)>, userid: #<BigDecimal:adb165,'1234.0',4(8)>, name: "asdfadsf", status: #<BigDecimal:15978e7,'1234.0',4(8)>, created_at: "2009-12-15 00:49:37", updated_at: "2009-12-15 00:49:37">

即使它抛出错误,它仍然会创建记录。当我尝试查看记录时,我得到以下堆栈,这实际上是相同的错误。

ActionController::RoutingError in Jobs#show

Showing app/views/jobs/show.html.erb where line #22 raised:

edit_job_url failed to generate from :controller=>"jobs", :action=>"edit", :id=>#<Job id: #<BigDecimal:18caa36,'10000.0',1(8)>, oid: #<BigDecimal:1fac733,'1324.0',4(8)>, userid: #<BigDecimal:12c1472,'1234.0',4(8)>, name: "asdfadsf", status: #<BigDecimal:f25f89,'1234.0',4(8)>, created_at: "2009-12-15 00:49:37", updated_at: "2009-12-15 00:49:37">, expected: :controller=>"jobs", :action=>"edit", diff: :id=>#<Job id: #<BigDecimal:1b9cdfc,'10000.0',1(8)>, oid: #<BigDecimal:1829097,'1324.0',4(8)>, userid: #<BigDecimal:e2d663,'1234.0',4(8)>, name: "asdfadsf", status: #<BigDecimal:691ccf,'1234.0',4(8)>, created_at: "2009-12-15 00:49:37", updated_at: "2009-12-15 00:49:37">
Extracted source (around line #22):

19: </p>
20: 
21: 
22: <%= link_to 'Edit', edit_job_path(@job) %> |
23: <%= link_to 'Back', jobs_path %>
RAILS_ROOT: /opt/code/import

Application Trace | Framework Trace | Full Trace
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:426:in `raise_named_route_error'
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:387:in `generate'
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/url_rewriter.rb:205:in `rewrite_path'
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/url_rewriter.rb:184:in `rewrite_url'
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/url_rewriter.rb:162:in `rewrite'
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:634:in `url_for'
/opt/jruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/url_helper.rb:85:in `url_for'
(eval):16:in `edit_job_path'
/opt/code/import/app/views/jobs/show.html.erb:22:in `_run_erb_app47views47jobs47show46html46erb'
/opt/code/import/app/controllers/jobs_controller.rb:18:in `show'
Request

Parameters:

"id"=>"10000"
Show session dump

Response

Headers:

"Cache-Control"=>"no-cache",
 "Content-Type"=>"text/html" 

当我删除“edit_job_path”方法时,错误消失了,所以我知道它只是在渲染路线时遇到了问题,但我不知道为什么,因为它似乎有正确的信息。我的意思是这是一个样板脚手架,所以....提前感谢您的帮助!

【问题讨论】:

【参考方案1】:

这是因为您的 ID 字段是 BigDecimal,而它应该是 Integer。它将诸如“1234.54”之类的数字解释为两部分,例如:action =&gt; "1234", :format =&gt; "54"

【讨论】:

宾果游戏。谢谢瑞恩!在我发布 BigDecimal 之后,我意识到它发生了一些事情,但不确定是什么。你搞定了。这很高兴,因为我使用的是 JRuby,我不需要这样做,所以我要切换回非 Java Ruby VM。【参考方案2】:

确保你的 config/routes.rb 中有这一行:

map.resources :jobs

同时运行“rake routes”以查看可用的路由。 *path 和 *url 方法由您在 routes.rb 文件中定义的命名路由和资源生成。更多信息here。

【讨论】:

以上是关于Rails 路由错误的主要内容,如果未能解决你的问题,请参考以下文章

Rails路由错误5

Rails 生产静态文件路由错误

rails 3.1资产管道路由错误

带有 Webpack 和 Yarn 的 Rails 6 上的 ActionController 路由错误

错误后渲染编辑时,Rails 将 id 附加到单个路由

Rails 4 RoutingError:没有路由匹配 [POST]