0
Rails中是否有任何方式刪除和重定向不同於控制器銷燬操作中定義的內容?Rails - link_to - 刪除動作 - 如何使用鏈接內的另一個控制器動作重定向到另一個頁面
在交易放映視圖我有:
<% @deal.tenants.each do |guy| %>
<li>Tenant id #<%= guy.id %> - <%= link_to guy.last_name, deal_tenant_path(@deal, guy) %>
<%= link_to "delete", deal_tenant_path(@deal, guy), method: :delete, data: { confirm: "Are you sure you want to delete this tenant?"} %></li>
<% end %>
上的承租者放映視圖我有:
<li><%= link_to "Delete Tenant", [@deal, @tenant], method: :delete,
data: { confirm: "Are you sure you want to delete this tenant?"},
class: "delete" %></li>
上的承租者索引視圖我有
<% @deal.tenants.each do |tenant| %>
<li>Tenant id #<%= tenant.id %> - <%= link_to tenant.last_name, [@deal, tenant] %></li>
<li><%= link_to "Delete Tenant", deal_tenant_path(tenant.deal, tenant), method: :delete, data: { confirm: "Are you sure you want to delete this tenant?"} %></li>
<% end %>
在行與我的控制器,我刪除時重定向到@deal秀。 tenants_controller.rb
def destroy
@tenant.destroy
flash[:notice] = "Tenant has been deleted."
redirect_to @deal
end
如果我想每次都以不同的方式重定向什麼?假設我想在該頁面上刪除時保留租戶顯示視圖,而不是將其重定向到交易展示頁面。或者我想重定向到租戶索引視圖等
我是否需要在租戶控制器中定義具有不同重定向的不同操作,或者我可以在link_to內部執行此操作嗎?
嵌套的路線:
resources :deals, only: [:index, :show, :create, :update, :destroy] do
scope '/siteadmin' do
resources :tenants
end
end
scope '/siteadmin' do
resources :deals, except: [:index, :show]
end
Deal.rb
class Deal < ApplicationRecord
has_many :tenants, dependent: :destroy
end
Tenant.rb
class Tenant < ApplicationRecord
belongs_to :deal
validates :deal_id, :first_name, :last_name, :age, presence: true
end
非常感謝你是這是我正在尋找的解釋。 – nicodo