respond_to 做|格式化| format.js 在 rails 6.1.3 中不工作
respond_to do |format| format.js not working in rails 6.1.3
application.js
require("@rails/ujs").start()
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
require("bootstrap");
require("jquery");
require('popper.js');
require("channels/jquery.nice-select.min.js");
require("channels/owl.carousel.min.js");
require("@fortawesome/fontawesome-free");
contacts_conttroller.rb
class ContactsController < ApplicationController
def index
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
ContactMailer.with(contact: @contact).notification.deliver_now
format.html {redirect_to contacts_path}
format.js { }
else
format.html {render :index}
format.json {render json: @contact.errors, status: :unprocessable_entity}
end
end
end
private ##
def contact_params
params.require(:contact).permit(:name, :phone_number, :message, :email)
end
end
因为我想看看 format.js 是否会 运行 所以它只有 1 行
create.js.erb
console.log('sending alert!');
index.html.haml
.main_contact_inner
= form_for @contact , url: contacts_path do |f|
- if @contact.errors.any?
%ul
- @contact.errors.full_messages.each do |msg|
%li= msg
#contactForm.row.contact_form
.form-group.col-md-4
= f.text_field :name, class: 'form-control', placeholder: 'Tên'
.form-group.col-md-4
= f.text_field :phone_number, class: 'form-control', placeholder: 'Số Điện Thoại'
.form-group.col-md-4
= f.text_field :email, class: 'form-control', placeholder: 'Email'
.form-group.col-md-12
= f.text_area :message, class: 'form-control', placeholder: 'Tin nhắn', rows: 1
.form-group.col-md-12
%button.btn.submit_btn.red.form-control{:type => "submit", :value => "submit"} Gửi đi
创建联系人后,控制台上没有显示任何日志。
我不知道为什么我的代码不能进入format.js,谁能帮我解决这个问题?
或者有人可以告诉我如何在不使用控制器的情况下创建像 sweetalert2 这样的警报。
我想你不见了remote: true
= form_for @contact , url: contacts_path, remote: true do |f|
:remote - if set to true, will allow Unobtrusive Javascript drivers to
control the submit behavior
这就是将 js header 添加到请求并允许 rails 控制器以 js 格式响应的原因。
<%= form_with(model: @article, id: "new-article", local: false) do |form| %>
<% end %>
添加local: false
application.js
require("@rails/ujs").start()
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
require("bootstrap");
require("jquery");
require('popper.js');
require("channels/jquery.nice-select.min.js");
require("channels/owl.carousel.min.js");
require("@fortawesome/fontawesome-free");
contacts_conttroller.rb
class ContactsController < ApplicationController
def index
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
ContactMailer.with(contact: @contact).notification.deliver_now
format.html {redirect_to contacts_path}
format.js { }
else
format.html {render :index}
format.json {render json: @contact.errors, status: :unprocessable_entity}
end
end
end
private ##
def contact_params
params.require(:contact).permit(:name, :phone_number, :message, :email)
end
end
因为我想看看 format.js 是否会 运行 所以它只有 1 行
create.js.erb
console.log('sending alert!');
index.html.haml
.main_contact_inner
= form_for @contact , url: contacts_path do |f|
- if @contact.errors.any?
%ul
- @contact.errors.full_messages.each do |msg|
%li= msg
#contactForm.row.contact_form
.form-group.col-md-4
= f.text_field :name, class: 'form-control', placeholder: 'Tên'
.form-group.col-md-4
= f.text_field :phone_number, class: 'form-control', placeholder: 'Số Điện Thoại'
.form-group.col-md-4
= f.text_field :email, class: 'form-control', placeholder: 'Email'
.form-group.col-md-12
= f.text_area :message, class: 'form-control', placeholder: 'Tin nhắn', rows: 1
.form-group.col-md-12
%button.btn.submit_btn.red.form-control{:type => "submit", :value => "submit"} Gửi đi
创建联系人后,控制台上没有显示任何日志。
我不知道为什么我的代码不能进入format.js,谁能帮我解决这个问题?
或者有人可以告诉我如何在不使用控制器的情况下创建像 sweetalert2 这样的警报。
我想你不见了remote: true
= form_for @contact , url: contacts_path, remote: true do |f|
:remote - if set to true, will allow Unobtrusive Javascript drivers to control the submit behavior
这就是将 js header 添加到请求并允许 rails 控制器以 js 格式响应的原因。
<%= form_with(model: @article, id: "new-article", local: false) do |form| %>
<% end %>
添加local: false