我对遥控器感到困惑:在Rails窗体中是这样的,我认为需要一些JavaScript来使其成为异步的,但这似乎只是打破了我的页面。Rails和AJAX remote:true还需要什么?
这是一个非常简单的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'
这就是前面提到的部分:
[email protected] do |a|
%h3= a.title
%p= a.appt_time
索引和创建控制器方法:
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
。那么我需要添加其他什么来处理请求?我是否违反了最佳做法,不包括处理此请求的内容并完全依赖于远程:是真的?
谢谢!在<%= j render“appointements”%>'中的“j”是什么? – tfantina
由于ERB是文件嵌入,所有Ruby代码都被转换为文件内容。我们希望文件内容是一个JS字符串“
”。这将告诉ERB不要把它写成文件内容,而应该写成JS代码:)如果我们没有,并且HTML部分内容有一个''/ n“'换行符,那么这将成为文件中的换行符,而不是一个包含'“/ n”'的JS字符串。 – fbelanger