RoR:无法更新模型

时间:2012-08-31 19:37:15

标签: ruby-on-rails rest routes partial-views put

我尝试使用其中一个模型的更新表单创建部分,但是当我运行服务器并使用部分浏览页面时,我得到一个奇怪的例外:

NoMethodError in Simpadmin/transactions#index

Showing /home/ben/proj/Simplee/master/app/views/simpadmin/transactions/_transaction_actions.html.haml where line #17 raised:

undefined method `payments_transaction_path' for #<#<Class:0x00000007bf1768>:0x00000007bceb00>
Extracted source (around line #17):

14:   %span.refund-success= flash[:refund_success]
15: 
16: .refund-edit
17:   = form_for transaction do |refund_form|
18:     .refund-reason
19:       = refund_form.label(:refund_reason, "Reason for refund:")
20:       = select_tag(:refund_reason, options_for_select(possible_refund_emails), :class => 'refund-reason-select') 
Trace of template inclusion: app/views/simpadmin/transactions/_

item.html.haml, app/views/simpadmin/transactions/index.html.haml

我的溃败可能有问题(?) 我应该改变部分内容吗?

编辑:路线不是我写的。无论如何,这是交易的路线部分:

 resources :transactions, :only => [:index, :update, :show] do                                                        
155       collection do                                                                                                      
156         get :export                                                                                                      
157         post :edit_cashed_checks                                                                                         
158         put :update_cashed_checks                                                                                        
159       end                                                                                                                
160       member do                                                                                                          
161         match :update_payee, :via => [:post, :put]                                                                       
162         match :add_comment, :via => :post                                                                                
163         put :refund                                                                                                      
164       end                                                                                                                
165     end    

2 个答案:

答案 0 :(得分:1)

您尚未为此表单定义路线。您必须在路径文件中将其定义为:

resources :payments do
  resources :transactions
end

有关详细信息,请参阅Routing Guide

答案 1 :(得分:1)

当您使用某个模型的form_for和传递对象时,在这种情况下,rails隐式假设使用帮助程序例程调用该操作。 Rails使用xyzs_path帮助程序找出匹配的路由,其中​​Xyz是我们传递给form_for的对象的模型名称。这意味着应该有

 resources :xyzs

匹配命名路由

 match 'some_url' => 'some_controller#action', :as => 'xyzs'  

在您的情况下,您需要定义

 resources :payments_transaction

命名路线

 match 'some_url' => 'some_controller#action', :as => 'payments_transaction'