0

In my UI, I have a table of items. Each row has a favorite button. I am using button to make a rails call to add the db association.

I've been looking into how to check the result of the post response in my button to call.

If it succeeds, I want to disable the favorite button on my page and if it doesn't I want to nothing. Is there a way to check the post response in my button to call and is there a way to stay on the page?

Heres a sample of what I have

<%= button_to create_favorite_path(res.id), class: "favorite-btn-#{res.id} btn btn-default" do%>
  <i class="glyphicon glyphicon-star-empty"></i>
<% end %>
4

1 回答 1

0

要在单击按钮时停留在页面上,您需要添加remote: true到按钮调用中。这将使您的按钮默认请求 AJAX 调用。

在此处阅读有关button_to助手的更多信息。

<%= button_to create_favorite_path(res.id), remote: true, class: "favorite-btn-#{res.id} btn btn-default" do%>
  <i class="glyphicon glyphicon-star-empty"></i>
<% end %>

然后要查看此 AJAX 请求的响应,您需要编写一些 javascript。

在您application.js或您在此页面上拥有的任何 javascript 文件中:

$(document).on('ajax:success', 'form', function(event, data, status, xhr) {
  // do something with data or status
});

在此处阅读有关在 Rails 中使用 javascript 的更多信息

于 2015-11-04T19:53:02.437 回答