我有一个模型Patients
有很多Healthplans
. 当单击“添加保险计划”链接时,我正在尝试通过使用 jquery 插入适当的表单来更新患者的健康计划。
单击时正确插入字段,但是提交表单时,出现以下错误:
No route matches [POST] "/patients/1472"
当我检查 form_for 生成的代码时,它没有使用 PUT 方法创建隐藏字段来覆盖默认的 POST 方法。这是导致错误的原因,因为该 url 没有 POST 路由。有趣的是,如果我直接在 show.html.erb 中对表单进行编码,它会正确显示并且正确生成 PUT 方法的隐藏字段。只有当我使用 jquery 添加表单部分时它才会失败。
我花了几个小时来调整代码,研究 StackOverflow 和全能的谷歌,还没有发现完全相同的问题。
这是我的表格:
<%= form_for @patient do |f| %>
<%= fields_for :healthplans_attributes do |builder| %>
<td><%= builder.text_field :company %></td>
<td><%= builder.hidden_field :_destroy %><%= link_to_remove_fields "remove", f %></td>
<% end %>
<td><%= f.submit "Submit" %></td>
<% end %>
我的控制器(省略了无关功能):
class PatientsController < ApplicationController
helper_method :sort_column, :sort_direction
def index
def show
@patient = Patient.find(params[:id])
@insurance = Patient.return_insurances(@patient)
@pa = @patient.preauthorizations
@programs = @patient.programs
@billingitems = Claim.return_billingitems(@patient, params[:page])
@address = Patient.return_address(@patient)
@phone = Patient.return_phone(@patient)
end
def new
@patient = Patient.new
1.times { @patient.healthplans.build }
1.times { @patient.preauthorizations.build }
1.times { @patient.programs.build }
render :url => 'new.html' , :layout => "false"
end
def new_insurance
@patient = Patient.find(params[:id])
respond_to do |format|
format.html
format.js { @patient }
end
end
def create
# raise params.inspect
@patient = Patient.new(params[:patient])
if @patient.save
flash[:notice] = "Successfully created patient."
redirect_to @patient
else
render :action => 'new'
end
end
def edit
@patient = Patient.find(params[:id])
end
def update
@patient = Patient.find(params[:id])
if @patient.update_attributes(params[:patient])
flash[:notice] = "Successfully updated patient."
redirect_to @patient
else
render :action => 'edit'
end
end
在我看来 show.html.erb,我添加保险计划的链接:
<tr>
<td colspan="7">
<table id="insurances"> </table>
</td>
<tr>
<td>
<%= link_to "Add Insurance Plan", new_insurance_patient_path(:id => @patient.id), :remote => true %>
</td>
</tr>
new_insurance.js.erb 文件:
J(function(){
var html = '<%= escape_javascript(raw render(:partial => 'add_insurance')) %>';
J("#insurances").append(html);
});
呈现部分 _add_insurance.html.erb:
<tr class="fields" >
<%= form_for @patient do |f| %>
<%= fields_for :healthplans_attributes do |builder| %>
<td><%= builder.text_field :company %></td>
<td><%= builder.hidden_field :_destroy %><%= link_to_remove_fields "remove", f %></td>
<% end %>
<td><%= f.submit "Submit" %></td>
<% end %>
</tr>
最后,我的 routes.rb 文件:
Billspace::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
match 'patients/set_active_flag/:id', :to => 'patients#set_active_flag'
match 'patients/active_list', :to =>'patients#active_list'
match 'patients/inactive_list', :to => 'patients#inactive_list'
match 'payments/show_payment_items/:id', :to => 'billingitems#show_payment_items'
match 'claims/claiminfo/:id', :to => 'claims#claiminfo'
match 'claiminfo/:id', :to => 'claims#claiminfo'
match 'patients/new', :to => 'patients#new'
match 'claims/new/create_claims', :to => 'claims#create_claims'
match 'payments', :to => 'billingitems#index'
resources :billingitems
resources :claims
resources :providers
resources :patients
resources :patients do
member do
get 'new_program'
get 'new_insurance'
end
end
match 'signup', :to => 'users#new', :as =>'signup'
match 'logout', :to =>'sessions#destroy', :as => 'logout'
match 'login', :to => 'sessions#new', :as => 'login'
resources :sessions
resources :users
end
谢谢。