我对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/