gpt4 book ai didi

javascript - 向 Rails Controller 指示哪个 View 调用了操作

转载 作者:行者123 更新时间:2023-12-03 09:36:05 25 4
gpt4 key购买 nike

我有两个 View ,它们提交类似的 Controller 创建操作,允许用户“跟踪”产品。单击“Track”按钮后,每个 View 都会通过 remote: true 属性提交 AJAX 请求,并且 javascript 响应应重新呈现调用它的相应部分。如何向我的 create.js.haml 脚本指示哪个 View 调用了该操作,然后做出相应的响应?

代码:

部分#1

- if current_user.present? && current_user.tracked_products.include?( @media )
%a.btn.btn-responsive.btn-thin.btn-color.red-bg{ href: main_app.price_tracker_path( current_user.price_trackers.where( product_id: @media.id ).first, _method: :delete ), style: 'font-size: 1.2em', data: { method: :delete, remote: true } }
-# %i.fa.fa-eye-slash
Stop Tracking
- elsif current_user.present?
%a.btn.btn-responsive.btn-thin.btn-color.brand-background-color-lavender{ href: main_app.modal_new_price_trackers_path( media_id: @media.slug ), style: 'font-size: 1.2em', data: { toggle: :modal, target: '#new_price_tracker' } }
-# %i.fa.fa-eye
Track It
- else
%a.btn.btn-responsive.btn-thin.btn-color.brand-background-color-lavender{ href: '#', style: 'font-size: 1.2em', data: { toggle: :modal, target: '#login_modal' } }
-# %i.fa.fa-eye
Track It

部分#2

- if (tracker = current_user.price_trackers.active.where( product: product ).first).present?
%a.btn-responsive.brand-color-white{ href: main_app.price_tracker_path( tracker, _method: :delete), data: { method: :delete, remote: true} }
%i.fa.fa-check-circle
Track
- else
%a.btn-responsive.brand-color-white{ href: main_app.modal_new_price_trackers_path( media_id: product.slug), data: { toggle: :modal, target: '#new_price_tracker' } }
Track

创建.js.haml:

if (//some indicator here) {
$('#track-product-'+#{@product.id}).html("#{j render(partial: 'products/track_item_from_card', locals: {product: @product})}");
else {
$('#track-item-button').html("#{j render(partial: 'products/track_item')}"); }
}

$('#new_price_tracker').attr("aria-hidden", "true");
$('#new_price_tracker').attr("style", "display: none;");

最佳答案

在一个 View 中,您应该这样放置。

== button_tag 'Save', :value => 'view1', :name => 'commit', :type => 'submit'

在第二个 View 中

== button_tag 'Save', :value => 'view2', :name => 'commit', :type => 'submit'

然后在创建操作中检查 params[:commit] 以确定它是 View 1 还是 View 2。

关于javascript - 向 Rails Controller 指示哪个 View 调用了操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com