【问题标题】:Proper way to perform AJAX call in Rails在 Rails 中执行 AJAX 调用的正确方法
【发布时间】:2013-04-16 16:22:26
【问题描述】:

我目前正在以“oldschool php way”执行 AJAX:

/home/index.html

  <%= button_to 'Get message', :id => 'get_message' %>
  <div id="message"></div>

assets/javascript/index.coffee

$(document).ready ->
  $('#get_message').on(
    'click',
    ->
      $.post(
        '/home/message/',
        (data) ->
          $('#message').text(data.message)
      )
  )

app/controllers/home

def message
  respond_to do |format|
    format.json { render :json => { :message => 'Hello, world!' } }
  end
end

我的做法是一种好的做法还是有更好的方法?谢谢!

【问题讨论】:

  • 查找 link_to_remote 功能,它是 rails 3 中的替代品。

标签: ruby-on-rails ajax coffeescript


【解决方案1】:

为此,您可以在button_toform_forlink_to 标签中使用:remote 选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多