我正在复制看似相同的逻辑,但它不适用于我的一个模型。
在调查中,我有
看法
<% @surveys.each do |survey| %>
...
<%= link_to 'Delete', survey, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
控制器
def destroy
@survey = Survey.find(params[:id])
@survey.destroy
respond_to do |format|
format.html { redirect to '/' }
format.json { head :no_content }
end
end
删除功能工作正常。
然而问题是,我有
看法
<% @questions.each do |question| %>
...
<%= link_to 'Delete', question, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
控制器
def destroy
@survey = Survey.find(params[:survey_id])
@question = Question.find(params[:id])
@question.destroy
respond_to do |format|
format.html { redirect to @survey }
format.json { head :no_content }
end
end
这给了我错误:
undefined method `question path' for #<#<Class:0x008ff2534....
当我删除 时link_to
,它可以很好地检索question
及其属性。
将我认为的逻辑更改为更具体的内容,
<%= link_to "Delete", :controller => "questions", :action => "destroy", :id => question.id %>
我得到一个更具体的错误。
No route matches {:controller=>"questions", :action=>"destroy", :id=>1}
运行rake routes
,这确认路径存在。
DELETE /surveys/:survey_id/questions/:id(.:format) questions#destroy
这是我的 routes.rb 条目:
devise_for :users do
resources :surveys do
resources :questions do
resources :responses
end
end
end
计算机不会出错,那我做错了什么?