首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

寻找action出错,该如何处理

2012-02-05 
寻找action出错有一个表叫news,index页面有如下一行代码生成链接% link_to new, new_news_path %点击

寻找action出错
有一个表叫news,
index页面有如下一行代码生成链接
<%= link_to 'new', new_news_path %>

点击这个代码生成的链接(http://localhost:3000/news/new),发生如下错误,看样子是去找到了show这个action。

Java code
Processing NewsController#new (for 127.0.0.1 at 2010-04-29 20:47:23) [GET]  News Columns (15.6ms)   SHOW FIELDS FROM `news`Rendering template within layouts/newsRendering news/newActionView::TemplateError (news_url failed to generate from [color=#FF0000]{:controller=>"news", :action=>"show"}[/color] - you may have ambiguous routes, or you may need to supply additional parameters for this route.  content_url has the following required parameters: ["news", :id] - are they all satisfied?) on line #3 of app/views/news/new.html.erb:1: <h1>New news</h1>2: 3: <% form_for(@news) do |f| %>4:   <%= f.error_messages %>5: 6:   <p>    (eval):16:in `news_path'    app/views/news/new.html.erb:3    app/controllers/news_controller.rb:30:in `new'Rendered rescues/_trace (109.2ms)Rendered rescues/_request_and_response (0.0ms)Rendering rescues/layout (internal_server_error)


路由的信息如下:
puts rs.routes
puts rs.routes
GET /news(.:format)? {:action=>"index", :controller=>"news"}
POST /news(.:format)? {:action=>"create", :controller=>"news"}
GET /news/new(.:format)? {:action=>"new", :controller=>"news"}
GET /news/:id/edit(.:format)? {:action=>"edit", :controller=>"news"}
GET /news/:id(.:format)? {:action=>"show", :controller=>"news"}
PUT /news/:id(.:format)? {:action=>"update", :controller=>"news"}
DELETE /news/:id(.:format)? {:action=>"destroy", :controller=>"news"}
ANY /:controller/:action/:id/ {}
ANY /:controller/:action/:id(.:format)? {}
=> nil
>> 


到底是什么原因呢?

[解决办法]
这是因为你用了一个模糊的数据表名news,rails不能用它自动的识别方式。

如果你一定要用这个做为 名称的话,可以在routes中加入这句

map.resources :news,:singular => :new

或者在你的<% form_for @news do |f| %> 写明controller和action
<%form_for @news, :url => {:controller => "news", :action => "create"}

个人建议你换个表名称,比如articles

热点排行