Ich versuche eine Anlage aus meinem Projekt Schienen zu löschen. Ich habe den Anhang unter test_suite verschachtelt. Wenn ich auf die Schaltfläche zum Löschen des Anhangs klicke, wechseln die generierten URLs die IDs von Anhang und test_suite. Ex. Die URL sollte localhost: 3000/test_suites/3/attachments/11 sein, aber ich bekomme localhost: 3000/test_suites/11/attachmnts/3 und ich bekomme den Fehler, dass Anhang ID = 3 nicht existiert, was richtig ist, weil es sollte Anlage ID = 11 seinMeine verschachtelte Beziehung wechselt die IDs der Modelle
Hier ist, wie ich es eingerichtet habe. routes.rb:
resources :test_suites do
resources :attachmnts, :only => [:create, :new, :destroy]
end
Modell/test_suite.rb:
class TestSuite < ApplicationRecord
has_many :attachemnts
end
Modell/attachment.rb:
class Attachment < ApplicationRecord
belongs_to :test_suite
has_attached_file :attach
validates_attachment_content_type :attach, :content_type => ["text/xml", "text/plain","text/html"]
end
ich neue Anlage in test_suite show.The Befestigung zu schaffen wird ein test_suite_id Attribut haben. test_suites_controller.rb:
def show
# create a variable pammed to test_suite with id passed from previous page
@test_suite = TestSuite.find(params[:id])
@attachment = Attachment.new
@attachment.test_suite_id = @test_suite.id
end
test_suite/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>
Attachments/_attachment.html.erb:
<% 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 %>
Meine Schienen Routen Ausgänge:
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
Ich sehe einen Tippfehler im Wort "Anhänge" aber in Ihrem routes.rb. Bitte aktualisieren Sie Ihren Code. –