2010-05-12 19 views
2

確定這裏的路徑是一些代碼:走樣路線導致軌道的期望,不存在

prompt>rails my_app 
prompt>cd my_app 
prompt>script/generate scaffold service_type title:string time_allotment:integer 
prompt>rake db:migrate 

然後編輯這些文件看起來像這樣:

#routes.rb: 
ActionController::Routing::Routes.draw do |map| 
    map.resources :services, :controller => :service_types 
    map.connect ':controller/:action/:id' 
    map.connect ':controller/:action/:id.:format' 
end 

產生這些路線:

prompt>rake routes 
    services GET /services(.:format)    {:controller=>"service_types", :action=>"index"} 
      POST /services(.:format)    {:controller=>"service_types", :action=>"create"} 
new_service GET /services/new(.:format)   {:controller=>"service_types", :action=>"new"} 
edit_service GET /services/:id/edit(.:format)  {:controller=>"service_types", :action=>"edit"} 
    service GET /services/:id(.:format)   {:controller=>"service_types", :action=>"show"} 
      PUT /services/:id(.:format)   {:controller=>"service_types", :action=>"update"} 
      DELETE /services/:id(.:format)   {:controller=>"service_types", :action=>"destroy"} 
        /:controller/:action/:id   
        /:controller/:action/:id(.:format) 

_

#my_app/app/views/service_types/index.html.erb 
<h1>Listing service_types</h1> 

<table> 
    <tr> 
    <th>Title</th> 
    <th>Time allotment</th> 
    </tr> 

<% @service_types.each do |service_type| %> 
    <tr> 
    <td><%=h service_type.title %></td> 
    <td><%=h service_type.time_allotment %></td> 
    <td><%= link_to 'Show', service_type %></td> 
    <td><%= link_to 'Edit', edit_service_path(service_type) %></td> 
    <td><%= link_to 'Destroy', service_type, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New service_type', new_service_path %> 

-

#my_app/app/views/service_types/new.html.erb 
<h1>New service_type</h1> 

<% form_for(@service_type) do |f| %> 
    <%= f.error_messages %> 

    <p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
    <%= f.label :time_allotment %><br /> 
    <%= f.text_field :time_allotment %> 
    </p> 
    <p> 
    <%= f.submit 'Create' %> 
    </p> 
<% end %> 

<%= link_to 'Back', services_path %> 
當您嘗試訪問 http://localhost:3000/services/new您會收到以下錯誤

undefined method `service_types_path' for #<ActionView::Base:0xb7199a80> 

提取的源(左右線#3):

1: <h1>New service_type</h1> 
2: 
3: <% form_for(@service_type) do |f| %> 
4: <%= f.error_messages %> 
5: 
6: <p> 

應用程序跟蹤:

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `__send__' 
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `polymorphic_url' 
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:114:in `polymorphic_path' 
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:298:in `apply_form_for_options!' 
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for' 
/home/aaron/NetBeansProjects/my_app/app/views/service_types/new.html.erb:3:in `_run_erb_app47views47service_types47new46html46erb' 
/home/aaron/NetBeansProjects/my_app/app/controllers/service_types_controller.rb:29:in `new' 

任何人都知道爲什麼它認爲service_types_path不在我的代碼中?

回答

1
<% form_for(@service_type) do |f| %> 

刪除與

<% form_for(@service_type, :url => services_path, :html => { :method => :post }) do |f| %> 
+0

真棒!感謝您的快速回復! – DJTripleThreat 2010-05-12 01:34:38

+2

以及如何猜測_form.html.erb中的方法? 「創建」應該發佈到/創建,而「更新」應該提交/更新。 – 2012-04-17 13:54:03

+0

@philpirozhkov,你讀過一個問題嗎?你在哪裏找到關於'_form.html.erb'的預言?主題作者詢問'new.html.erb'模板。 – fl00r 2012-04-17 14:30:32