0

抱歉,如果是菜鸟问题,这里已经出现了 100 次。我有一个文章列表,上面是“到购物车”按钮。我阅读了很多关于模态视图和rails的内容,但它们都是针对link_to的,如何做到这一点,如果我按下我的按钮,它会向我显示一个带有我的购物车的模态窗口?这是我的代码的一部分,带有按钮(它没有 form_tag 等,它位于:

- @articles.each do |art|
  = button_to 'В корзину', line_items_path(ART_ID: art), :id => "to-cart"

我想要,如果我点击它,控制器方法会做什么,但它不会重定向到任何地方,我仍然必须在同一页面上,并且模式视图(使用 html 和 jquery)必须出现在我的购物车部分:

-unless cart.line_items.empty?
  %table.zebra
    %tr
      %th Наименование
      %th Артикул
      %th Производитель
      %th Количество 
      %th Сумма
    = render(cart.line_items)
  %p
    Общая сумма
    = cart.total_price
  %p
    Наименований (количество):
    = cart.total_count
  = button_to 'Очистить корзину', cart, method: :delete, confirm: 'Вы уверены?'
  -if current_user
    = button_to "Оформить заказ", new_order_path, method: :get
  -else
    %p
      Пожалуйста, для оформления заказа войдите в систему:
      %a#cartlogin{:href => "#"} Логин
    %p
      Если у вас нет аккаунта, зарегистрируйтесь:
      = link_to "Регистрация", new_user_path 
-else
  Увы, ваша корзина пуста
4

1 回答 1

0

The things you want can be done through jQuery/JavaScript. If you're looking for good looking modals, I suggest you checkout jQuery UI dialog.

You should first attach an event handler to the link, like so. Then disable the default action (which may be a redirect) by adding the event.preventDefault() line. Then you can request for the view data using ajax (probably in JSON format) and show the modal with the data.

$("#link").click(function(event) {
  event.preventDefault();

  //do an ajax call here and show modal
});

I suggest you check out these links:

http://api.jquery.com/click/

http://api.jquery.com/event.preventDefault/

http://api.jquery.com/load/

..or you can check out this plugin, I haven't tried it myself but it looks really easy to use.

https://github.com/kylefox/jquery-modal

于 2012-08-13T17:58:43.160 回答