Ruby on rails 我的嵌套关系正在切换模型的ID

Ruby on rails 我的嵌套关系正在切换模型的ID,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在尝试从rails项目中删除附件。我将附件嵌套在测试套件下。当我点击附件删除按钮时,生成的url切换附件和测试套件的ID。例如,url应该是localhost:3000/test_suites/3/attachments/11,但我得到的是localhost:3000/test_suites/11/attachments/3,我得到的错误是附件id=3不存在,这是正确的,因为它应该是附件id=11 下面是我如何设置它的。 routes.rb: resources :test_suites

我正在尝试从rails项目中删除附件。我将附件嵌套在测试套件下。当我点击附件删除按钮时,生成的url切换附件和测试套件的ID。例如,url应该是localhost:3000/test_suites/3/attachments/11,但我得到的是localhost:3000/test_suites/11/attachments/3,我得到的错误是附件id=3不存在,这是正确的,因为它应该是附件id=11

下面是我如何设置它的。 routes.rb:

  resources :test_suites do
    resources :attachmnts, :only => [:create, :new, :destroy]
  end
模型/测试套件.rb:

class TestSuite < ApplicationRecord
    has_many :attachemnts
end
测试套件/show.html.erb:

<table class="table table-hover table-responsive">
    <thead>
    <tr>
        <th>File Name</th>
        <th>File Type</th>
        <th>File Size</th>
        <th>Created At</th>
        <th></th>
    </tr>
    </thead>
    <tbody>

    <%= render partial: 'attachment', locals: {test_suite: @test_suite} %>
    </tbody>
</table>
<% test_suite.attachemnts.each do |attachment| %>
<tr>
    <td> <%=attachment.attach_file_name %> </td>
    <% if attachment.attach_content_type == 'text/plain'%>
        <td>txt</td>
    <% else %>
        <td><%= attchement.attach_content_type.split('/').last %></td>
    <% end %>
    <td><%= attachement.attach_file_size %></td>
    <td><%= attachement.created_at %></td>
    <td><%= link_to '<span class="glyphicon glyphicon-remove-sign"></span>'.html_safe, test_suite_attachment_path(attachment),class:"btn btn-lg", method: :delete, data: {confirm: "Are you sure you want to delete the file?"} %>
    </td>
</tr>
<% end %>

处理嵌套对象时,需要父对象和子对象的ID来执行显示/编辑/更新/销毁操作。问题是因为您没有将
test\u套件
对象传递给
test\u套件附件路径
helper。修改你的链接到

<%= link_to '<span class="glyphicon glyphicon-remove-sign"></span>'.html_safe, test_suite_attachement_path(test_suite, attachment),class:"btn btn-lg", method: :delete, data: {confirm: "Are you sure you want to delete the file?"} %>

您还可以传递父对象和子对象的数组,而不用使用路径辅助对象

#[test_suite, attachment]

<%= link_to '<span class="glyphicon glyphicon-remove-sign"></span>'.html_safe, [test_suite, attachment], class:"btn btn-lg", method: :delete, data: {confirm: "Are you sure you want to delete the file?"} %>    
#[测试套件,附件]
请使用“附件”的统一拼写。你在每个地方都使用不同的版本


我在“附件”一词中看到了拼写错误,但在your routes.rb中看到了拼写错误。请更新您的代码。
                   Prefix Verb   URI Pattern                                            Controller#Action
                      root GET    /                                                      test_suites#index
   test_suite_attachements POST   /test_suites/:test_suite_id/attachements(.:format)     attachements#create
new_test_suite_attachement GET    /test_suites/:test_suite_id/attachements/new(.:format) attachements#new
    test_suite_attachement DELETE /test_suites/:test_suite_id/attachements/:id(.:format) attachements#destroy
               test_suites GET    /test_suites(.:format)                                 test_suites#index
                           POST   /test_suites(.:format)                                 test_suites#create
            new_test_suite GET    /test_suites/new(.:format)                             test_suites#new
           edit_test_suite GET    /test_suites/:id/edit(.:format)                        test_suites#edit
                test_suite GET    /test_suites/:id(.:format)                             test_suites#show
                           PATCH  /test_suites/:id(.:format)                             test_suites#update
                           PUT    /test_suites/:id(.:format)                             test_suites#update
                           DELETE /test_suites/:id(.:format)                             test_suites#destroy
<%= link_to '<span class="glyphicon glyphicon-remove-sign"></span>'.html_safe, test_suite_attachement_path(test_suite, attachment),class:"btn btn-lg", method: :delete, data: {confirm: "Are you sure you want to delete the file?"} %>
#[test_suite, attachment]

<%= link_to '<span class="glyphicon glyphicon-remove-sign"></span>'.html_safe, [test_suite, attachment], class:"btn btn-lg", method: :delete, data: {confirm: "Are you sure you want to delete the file?"} %>