Javascript 在submit rails的模式对话框上显示flash消息
我正在尝试构建用于在rails中注册电子邮件地址的模式框。 我已经完成了,但无法找到在注册失败时在模式对话框中显示flash消息的方法。 提交时,框关闭,消息仅显示在主页上。我对web或rails没有很好的了解,但如果我能得到一些指导,那就太好了 该功能工作正常,除非出现故障,否则对话框应在其上显示消息,而不是在主页上显示消息。我知道我的代码中的问题在哪里,但不知道解决方法。这是我的对话框代码Javascript 在submit rails的模式对话框上显示flash消息,javascript,jquery,ajax,ruby-on-rails-4,modal-dialog,Javascript,Jquery,Ajax,Ruby On Rails 4,Modal Dialog,我正在尝试构建用于在rails中注册电子邮件地址的模式框。 我已经完成了,但无法找到在注册失败时在模式对话框中显示flash消息的方法。 提交时,框关闭,消息仅显示在主页上。我对web或rails没有很好的了解,但如果我能得到一些指导,那就太好了 该功能工作正常,除非出现故障,否则对话框应在其上显示消息,而不是在主页上显示消息。我知道我的代码中的问题在哪里,但不知道解决方法。这是我的对话框代码 <div id="register" class="modal fade" role="d
<div id="register" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<%= form_for(@email, url: home_esave_path, :html => {class:"model-dialog"}) do |f| %>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Register</h4>
</div>
<div class="modal-body">
<div class="form-group">
<%= f.text_field :email, class:"form-control", placeholder: "Enter a valid gmail address" %>
</div>
<%= f.show_simple_captcha(:email=>"email", :label => "Human Authentication", :refresh_button_text => "Refresh text", class:"form-control", :placeholder => "Enter the code") %>
<%= f.submit "Register me!", :class => "btn register_button" %>
</div>
<div class="modal-footer" style="text-align:left">
<!-- show in case of success -->
<% if @email.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@email.errors.count, "error") %> prohibited
this email from being saved:
</h2>
<ul>
<% @email.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<% end %>
</div>
</div>
结束
我在这个模型外显示的Flash消息很明显会显示在主页上。但我并没有找到在“模型”对话框中显示它的方法
下面是我显示消息的方式
def save
@email = Email.new(user_params)
if !user_params.nil?
if @email.save_with_captcha
flash[:success] = "Success! The email address #{@email.email} has been successfully registered. We will contact you as soon as we have available rooms for new beta testers."
redirect_to :action => 'index'
else
if @email.valid?
flash[:error] = "The captcha is not correct, enter again the captcha."
redirect_to :action => 'index'
else
flash[:error] = "You have put invalid #{@email.email}"
redirect_to :action => 'index'
end
end
end
<% flash.each do |type, message| %>
<div class="alert <%= alert_for(type) %> fade in">
<button class="close" data-dismiss="alert">×</button>
<span class="sr-only">Close</span>
<%= message %>
</div>
<% end %>
×
关
感谢您的建议闪存设计为在(控制器)操作之间传递数据的一种方式(有关更多信息,请参阅)。在你的情况下,你不想采取行动,所以我认为没有办法做到这一点 但是,您可以使用Ajax生成与其他flash消息类似的消息。这里有一种方法 您将需要以下组件: 来自modal的远程呼叫 将
remote:true
或:remote=>true
添加到模式中的表单中。这会告诉Rails,在提交表单时,您希望对服务器进行Ajax调用,而不是整页文章
控制器更改
您需要响应HTML以外的格式。大概是这样的:
def save
if !user_params.nil?
@email = Email.new(user_params)
@email.save_with_captcha
respond_to do |format|
format.html { redirect_to @email, notice: 'This should not happen.' }
format.json { render action: 'show' }
format.js { render action: 'show' }
end
end
end
Javascript在模式中插入/清除“flash”错误消息
查看文件-show.js.erb(可以使用不同的名称,但名称应与上面的操作匹配)
$(“#生成错误”).remove();
var build_error=“”;
生成错误+=“表单包含。”;
构建错误+=“”;
构建错误+=“”;
构建错误+=“
”;
生成错误+=“”;
$(生成错误).insertBefore(“#xxxxxx”);
$(“#电子邮件模式”).modal_success();
基本上,您需要清除任何以前的“flash”错误消息(第一行),然后显示新的错误消息(您需要在DOM中找到某个位置来附加它-请参见xxxxxx)或关闭模式。蛮蛮力的,但对我有用
希望你能理解这个想法——我怀疑这一切都会起作用,因此你需要理解这个概念。我不确定这能不能做到。Flash设计为在提供新页面时显示。我在我的一个应用程序中使用Ajax模式实现了这一点,基本上是在我的.js.erb视图文件中构建/设置flash消息的样式。如果有兴趣的话,我可以发布一个答案来解释这个方法。是的,如果我能找到方法,它应该足够让它工作了。尽管我不擅长这门语言。但至少有一些东西可以开始谢谢
$('#build_error').remove();
<% if @email.errors.any? %>
var build_error = "<div class='alert alert-danger' id='build_error'>";
build_error += "The form contains <%= pluralize(@email.errors.count, 'error') %>.";
build_error += "<ul id='error_explanation'>";
<% @email.errors.full_messages.each do |msg| %>
build_error += "<li><%= j msg %></li>";
<% end %>
build_error += "</ul>";
build_error += "</div>";
$(build_error).insertBefore( "#xxxxxx" );
<% else %>
$('#email-modal').modal_success();
<% end %>