我有两个视图提交类似的控制器创建操作,允许用户跟踪"跟踪"一个产品。点击" Track"按钮,每个视图通过remote:true属性提交一个AJAX请求,javascript响应应该重新呈现调用它的相应部分。如何向我的create.js.haml脚本指示哪个视图调用了该操作,然后相应地做出响应?
代码:
部分#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
create.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;");
答案 0 :(得分:1)
在一个视图中你应该这样说。
== button_tag 'Save', :value => 'view1', :name => 'commit', :type => 'submit'
在第二个视图中
== button_tag 'Save', :value => 'view2', :name => 'commit', :type => 'submit'
然后在您的创建操作中检查params[:commit]
,以确定它是view1还是view2。