What i have:
Routes:
resources :tests do
resources :resultsets, :only => [:create, :destroy]
resources :testresults, :only => [:edit, :update] do
resources :testnotes, :only => [:create, :update, :destroy]
end
end
Submitted form: (No additional variables)
<%= form_for [@session, @testresult, @testnote], :remote => true do |f| %>
<%= f.text_field :line %>
<% end %>
@session is a Test
@testresult is a Testresult
@testnote = Testnote.new
Controller Actions:
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
What is the error:
Everything, but the response, is working fine. The database is working correctly and the entries are being created as they should. But the browser throws me the following error:
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">}
My thoughts:
Short: I have absolutly no idea!
Obviously the routing is fine, else the server would not even reach the controller action and do those database entries. But what is creating the second routing request? And why isn’t the respons being rendered correctly?
Edit:
The form is submitting correctly and being routed properly to the create action, which is being called correctly aswell. And everything works until format.js. Could the problem be in the view?
Edit2: (View and partial)
Create.js
$('#notes_drop').closest('tr').before('<%= j render :partial => "testnotes/testnote", :locals => {:note => @testnote} %>');
testnotes/testnote partial
<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>
Update: Your view includes the line
test_testresult_testnote_path(@session, @testresult, note)Since
@sessionis nil, the Rails router can’t figure out how to generate thetest_testresult_testnote_path.Original answer below.
My guess is that your view code for format.js includes code that is trying to find a particular route. Maybe your js view code is trying to render the form partial?
In any case, if you look at
You’ll notice that
:test_id =>nil, which means that you’re not passing a Test instance for routing. Since your:testnotesare defined as a nested resource under:testresults, which is nested under:tests, you will have to pass a non-nil instances of TestResult and Test in order to generate a route correctly.Are you instantiating the @session variable in a before_filter? If not, try instantiating @session in your update action, and see if that fixes your problem.