0

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">&nbsp;</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

ありがとう。

4

1 に答える 1

1

これとまったく同じ問題が発生しました(Rails 3.2.8を使用しています)。あなたがこの質問をしてから6か月が経ち、おそらくそれを解決するか、あきらめてから長い年月が経ちましたが、将来のグーグルのためにこの回答を投稿します。

ここでの問題は、私と同じように、フォームタグが<tr>内にあり、 <td>内にはないことです。これは<form>タグには問題ありませんが、<div>タグなどの可視(または潜在的に可視)のコンテンツは通常そのような場所に配置されるべきではないため、レールが非表示の入力を囲むために使用する<div>タグには適していません。Railsは、明らかに.erbファイルからページを生成するときにこれを行うことに問題はありませんが、jQueryはそのようなばかげた、明らかに間違ったことをするように求められるのを嫌がり、問題のある<div>を黙って破棄します。そのすべての内容と一緒に。私のために働いた解決策は、フォームタグをそれ自体の小さな目に見えない<td>で囲むことでした。

<td style="display:none">
    <%= form_for @patient do |f| %>
</td>

これは、 <div>を<tr>の中に直接入れる狂人ではないというjQueryを満足させるようであり、フォームはPUTとして適切に送信されます。

于 2012-12-20T23:00:06.657 に答える