读书人

找寻action出错

发布时间: 2012-12-31 11:57:51 作者: rapoo

寻找action出错
本帖最后由 hjack 于 2010-04-29 20:57:42 编辑 有一个表叫news,
index页面有如下一行代码生成链接
<%= link_to 'new', new_news_path %>

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



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/news
Rendering news/new

ActionView::TemplateError (news_url failed to generate from {:controller=>"news", :action=>"show"} - 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

读书人网 >Ruby Rails

热点推荐