0

私が持っているもの:

ルート:

resources :tests do
  resources :resultsets, :only => [:create, :destroy]
  resources :testresults, :only => [:edit, :update] do
    resources :testnotes, :only => [:create, :update, :destroy]
  end
end

提出されたフォーム: (追加の変数はありません)

<%= form_for [@session, @testresult, @testnote], :remote => true do |f| %>
  <%= f.text_field :line %>
<% end %>

@sessionテスト
@testresultですテスト結果です
@testnote = Testnote.new

コントローラーのアクション:

def create
  @testresult = Testresult.find(params[:testresult_id])
  @testnote = Testnote.find_or_create_by_line(params[:testnote][:line])
  @connection = Testnoteconnection.find_or_initialize_by_testnote_id_and_testresult_id(@testnote.id, @testresult.id)

  respond_to do |format|
    if @connection.new_record? and @connection.save
      format.js
    else
      format.js { render :partial => 'error' }
    end
  end
end

def update
  @testresult = Testresult.find(params[:testresult_id])
  @testnote = Testnote.find(params[:id])
  @connection = Testnoteconnection.find_or_initialize_by_testnote_id_and_testresult_id(@testnote.id, @testresult.id)

  respond_to do |format|
    if @connection.new_record? and @connection.save
      format.js
    else
      format.js { render :partial => 'error' }
    end
  end
end

エラーは何ですか:

応答以外のすべてが正常に機能しています。データベースは正常に機能しており、エントリは正常に作成されています。しかし、ブラウザは私に次のエラーをスローします:

No route matches {:action=>"update", :controller=>"testnotes", :test_id=>nil, :testresult_id=>#<Testresult id: 13, resultset_id: 4, testobjecttype_id: 114, testtype_id: 1, result: nil, randomed_order: 0, created_at: "2012-11-28 16:22:49", updated_at: "2012-11-28 16:22:49">, :id=>#<Testnote id: 10, line: "asdf", created_at: "2012-12-05 16:06:17", updated_at: "2012-12-05 16:06:17">}

私の考え:

短い: まったくわかりません!
明らかに、ルーティングは問題ありません。そうしないと、サーバーはコントローラー アクションに到達して、それらのデータベース エントリを実行することさえできません。しかし、2 番目のルーティング要求を作成しているのは何ですか? また、応答が正しくレンダリングされないのはなぜですか?

編集:createフォームは正しく送信され、正しく呼び出されているアクションに 正しくルーティングされています。そして、すべてが まで機能しformat.jsます。問題はビューにありますか?

Edit2: (ビューと部分)

Create.js

$('#notes_drop').closest('tr').before('<%= j render :partial => "testnotes/testnote", :locals => {:note => @testnote} %>');

テストノート/テストノートの部分

<tr id='comment_<%= dom_id(note) %>'>
  <td>
    <%= note.line %>
  </td>
  <td>
    <%= link_to 'delete', test_testresult_testnote_path(@session, @testresult, note), :method => :delete, :remote => true %>
  </td>
</tr>
4

2 に答える 2

2

更新:ビューには行が含まれています test_testresult_testnote_path(@session, @testresult, note)

is nil であるため@session、Rails ルーターは を生成する方法を理解できませんtest_testresult_testnote_path

以下の元の回答。

私の推測では、format.js のビュー コードには、特定のルートを見つけようとするコードが含まれていると思います。js ビュー コードがフォームを部分的にレンダリングしようとしている可能性がありますか?

いずれにせよ、よく見ると

No route matches {:action=>"update", :controller=>"testnotes", :test_id=>nil, :testresult_id=>#<Testresult id: 13, resultset_id: 4, testobjecttype_id: 114, testtype_id: 1, result: nil, randomed_order: 0, created_at: "2012-11-28 16:22:49", updated_at: "2012-11-28 16:22:49">, :id=>#<Testnote id: 10, line: "asdf", created_at: "2012-12-05 16:06:17", updated_at: "2012-12-05 16:06:17">}

:test_id =>nilこれは、ルーティングのために Test インスタンスを渡していないことを意味します。は の下にネストされた の下に:testnotesネストされたリソースとして定義されているため、ルートを正しく生成するには、 TestResult と Test の nil 以外のインスタンスを渡す必要があります。:testresults:tests

before_filter で @session 変数をインスタンス化していますか? そうでない場合は、更新アクションで @session をインスタンス化してみて、問題が解決するかどうかを確認してください。

于 2012-12-05T20:01:28.413 に答える
1

ここでテストするための環境がセットアップされていないため、これは非常に推測的な答えです(しゃれ)。

私の賭けは、キーワードとのクラス/名前空間の競合Testです。少し面倒だと思いますが、何かに変更してみましたTestoか?あなたはその過程で何かを学ぶかもしれません、あなたの設定はよさそうです、そして私は以前にこの種のクラスの衝突を経験したので、それが私の推奨の由来です

于 2012-12-05T19:48:31.787 に答える