javascript - Rails 和 AJAX 远程 : true what else is required?

标签 javascript ruby-on-rails ajax

我对Rails 表单中的remote:true 感到困惑,我认为需要一些Javascript 才能使其异步,但这似乎破坏了我的页面。

这是一个非常简单的index.html.haml,其中包含显示所有约会的部分内容:

%h1 Calander

%h2 AppointmentsController
%h3 Make a new appointment

= form_for @appointment, remote: true do |f|
  = f.text_field :title
  = f.text_field :appt_time
  = f.submit 'Make appointment'

#appointments
  =render 'appointments'

这是前面提到的部分内容:

    -@appointments.each do |a|
  %h3= a.title
  %p= a.appt_time

索引和创建的 Controller 方法:

 def index
    @appointments = Appointment.order('appt_time ASC')
    @appointment = Appointment.new
  end

  def create
    @appointmet = Appointment.create(appointment_params)
    redirect_to :root
  end

现在效果很好。我可以添加一个新的约会,点击提交,新的约会就会显示而无需刷新页面,我想是因为我已经包含了 remote: true。那么我还需要添加其他内容来处理该请求吗?我是否违反了最佳实践,没有包含处理此请求的内容并完全依赖于远程:true?

最佳答案

除非您希望在 ajax 调用后进行一些回调,否则无需再做任何事情。你没有打破任何约定。您可以阅读此document摆脱困惑。

关于javascript - Rails 和 AJAX 远程 : true what else is required?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43562328/

相关文章:

javascript - 如何逐步向一个元素添加 2 个类

javascript - DOM 准备好后加载 iframe。文件准备好

javascript - TSLint ESLint - 当函数作为属性访问时发出警告?

mysql - Rails has_many/belongs_to 自定义数据库 ActiveRecord::AssociationTypeMismatch 得到 Fixnum

ruby-on-rails - 如何在不破坏 FactoryGirl 的情况下将 seeds.rb 加载到测试数据库中?

ruby-on-rails - 是否可以从 Rack 中间件调用 rails 模型方法?

javascript - 页面加载后如何运行 animate() ?

jquery - colorbox在firefox中多次加载同一文档

php - Javascript 数组中的当前条目

ajax - "User is typing"聊天功能