Rails中是否有任何方法可以删除和重定向与控制器销毁操作中定义的不同?
在交易节目视图中我有:
<% @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
答案 0 :(得分:0)
link_to无法完成,因为操作将在您的控制器中调用。但是你当然可以将redirect_更改为@deal到
redirect_to :back
或 redirect_to what_ever_path
您可以随时创建不同的操作,或者使用link_to传递参数,并在当前控制器操作中添加一行,如:
if params[:tenant]
redirect_to :back
else
redirect_to @deal
回答你的问题?