【问题标题】:avoid button_to action with javascript in rails避免在 rails 中使用 javascript 进行 button_to 操作
【发布时间】:2013-09-03 17:34:06
【问题描述】:

我正在制作购物车,用户只有在登录后才能将产品添加到购物车中。

当用户单击“添加到购物车”按钮时,我创建了一些 JavaScript,它会评估是否有人登录并显示消息,但我需要停止按钮操作,因为会导致渲染操作出现问题。 谢谢。这是我的代码

<%=button_to 'Add to cart',line_items_path(:product_id => product.id),:onclick=>"javascript:is_user()"%>

【问题讨论】:

    标签: javascript ruby-on-rails shopping-cart cart button-to


    【解决方案1】:

    你可以试试这样的:

    <% if current_user.present? %>
      <%= button_to 'Add to cart',line_items_path(:product_id => product.id) %>
    <% else %>
      <%= button_to 'Login to add to cart', '' %>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      相关资源
      最近更新 更多