Patients
たくさんあるモデルがありHealthplans
ます。[保険プランの追加]リンクをクリックしたときにjqueryを使用して適切なフォームを挿入することにより、患者の健康プランを更新しようとしています。
クリックするとフィールドが正しく挿入されますが、フォームを送信すると、次のエラーが発生します。
No route matches [POST] "/patients/1472"
form_forによって生成されたコードを確認すると、デフォルトのPOSTメソッドをオーバーライドするためにPUTメソッドで非表示フィールドが作成されていません。そのURLのPOSTルートがないため、これがエラーの原因です。興味深いことに、フォームをshow.html.erb内に直接コーディングすると、フォームは正しく表示され、PUTメソッドの非表示フィールドが適切に生成されます。jqueryで部分的なフォームを追加した場合にのみ失敗します。
私はコードを微調整し、StackOverflowとAlmighty Googleを調査して何時間も費やしましたが、まったく同じ問題についての言及は見つかりませんでした。
これが私のフォームです:
<%= 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
ありがとう。