2016-08-08 6 views
2

私は自分のレールプロジェクトから添付ファイルを削除しようとしています。私は、test_suiteの下にネストされた添付ファイルを持っています。添付ファイルの削除ボタンを押すと、生成されたURLが添付ファイルとtest_suiteのIDを切り替えます。例。 URLはlocalhost:3000/test_suites/3/attachments/11でなければなりませんが、localhost:3000/test_suites/11/attachmnts/3を取得しており、添付ファイルID = 3が存在しないというエラーが表示されますアタッチメントID = 11入れ子の関係でモデルのIDを切り替える

これは設定方法です。 routes.rbを:

resources :test_suites do 
    resources :attachmnts, :only => [:create, :new, :destroy] 
    end 

モデル/ test_suite.rb:

class TestSuite < ApplicationRecord 
    has_many :attachemnts 
end 

モデル/ 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 

私はtest_suite show.Theの添付ファイルで新しい添付ファイルを作成していますtest_suite_id属性を持ちます。 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> 

添付ファイル/ _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 %> 

マイレールルート出力:

    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 
+0

は、私は言葉「添付ファイル」ではなく、あなたのroutes.rbをしてタイプミスを参照してください。コードを更新してください。 –

答えて

1

ネストされたオブジェクトを扱う場合、表示/編集/更新/破棄操作を実行するには、親オブジェクトと子オブジェクトの両方のIDが必要です。問題はtest_suiteオブジェクトをtest_suite_attachment_pathヘルパーに渡さないためです。リンクを変更する

<%= 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?"} %>  

'添付ファイル' の一貫性のあるスペルを使用してください。すべての場所で異なるバージョンを使用しています。

+0

それは私のためのworkd素晴らしい。おかげでArun –

関連する問題