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
@session是一个测试@testresult是一个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?
编辑:表单正确提交并正确路由到创建操作,正在被正确调用。一切正常,直到format.js。这个问题可能在视野中吗?
Edit2: (View and partial)
Edit2 :(查看和部分)
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>
2 个解决方案
#1
2
Update: Your view includes the line test_testresult_testnote_path(@session, @testresult, note)
更新:您的视图包含行test_testresult_testnote_path(@session,@ testresult,note)
Since @session
is nil, the Rails router can't figure out how to generate the test_testresult_testnote_path
.
由于@session是nil,Rails路由器无法弄清楚如何生成test_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?
我的猜测是你的format.js的视图代码包含试图找到特定路线的代码。也许你的js视图代码试图将表单部分渲染?
In any case, if you look at
无论如何,如果你看一下
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">}
You'll notice that :test_id =>nil
, which means that you're not passing a Test instance for routing. Since your :testnotes
are 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.
您会注意到:test_id => nil,这意味着您没有传递Test实例进行路由。由于您的:testnotes被定义为嵌套资源:testresults,它嵌套在:tests下,您必须传递一个非零的TestResult和Test实例才能正确生成路由。
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.
你是否在before_filter中实例化@session变量?如果没有,请尝试在更新操作中实例化@session,看看是否能解决您的问题。
#2
1
This is a highly speculative answer as I don't have the environment setup here to test in (pun).
这是一个高度推测性的答案,因为我没有在这里设置环境来测试(双关语)。
My bet would be some Class/Namespace conflict with the keyword Test
. I know it is a bit of a pain, but did you try changing it to Testo
or something? You might learn something in the process, your configs look good, and I had this kind of class conflict before, so that's where my recommendation come from
我的赌注是与关键字Test的一些Class / Namespace冲突。我知道这有点痛苦,但你是否尝试将其改为Testo或其他什么?你可能会在这个过程中学到一些东西,你的配置看起来很好,之前我遇到过这种类冲突,所以这就是我推荐的来源
#1
2
Update: Your view includes the line test_testresult_testnote_path(@session, @testresult, note)
更新:您的视图包含行test_testresult_testnote_path(@session,@ testresult,note)
Since @session
is nil, the Rails router can't figure out how to generate the test_testresult_testnote_path
.
由于@session是nil,Rails路由器无法弄清楚如何生成test_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?
我的猜测是你的format.js的视图代码包含试图找到特定路线的代码。也许你的js视图代码试图将表单部分渲染?
In any case, if you look at
无论如何,如果你看一下
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">}
You'll notice that :test_id =>nil
, which means that you're not passing a Test instance for routing. Since your :testnotes
are 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.
您会注意到:test_id => nil,这意味着您没有传递Test实例进行路由。由于您的:testnotes被定义为嵌套资源:testresults,它嵌套在:tests下,您必须传递一个非零的TestResult和Test实例才能正确生成路由。
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.
你是否在before_filter中实例化@session变量?如果没有,请尝试在更新操作中实例化@session,看看是否能解决您的问题。
#2
1
This is a highly speculative answer as I don't have the environment setup here to test in (pun).
这是一个高度推测性的答案,因为我没有在这里设置环境来测试(双关语)。
My bet would be some Class/Namespace conflict with the keyword Test
. I know it is a bit of a pain, but did you try changing it to Testo
or something? You might learn something in the process, your configs look good, and I had this kind of class conflict before, so that's where my recommendation come from
我的赌注是与关键字Test的一些Class / Namespace冲突。我知道这有点痛苦,但你是否尝试将其改为Testo或其他什么?你可能会在这个过程中学到一些东西,你的配置看起来很好,之前我遇到过这种类冲突,所以这就是我推荐的来源