Ant*_*yev 16 ajax jquery haml ruby-on-rails bootstrap-modal
我正在尝试创建一个可重复使用的远程模态.我们的想法是创建一个模型容器并在其中呈现内容yield并在需要时呈现内容.
注意:所有HTML代码都将被写入HAML.我正在使用bootstrap-modal-rails gem来处理我的模态.
我的模态容器布局(不是部分):
%div(class="modal" id="mainModal ajax-modal" tabindex="-1" role="dialog"){:'aria-labelledby'=>"mainModalLabel", :'aria-hidden'=>"true"}
%div(class="c-modal__container")
%div(class="c-modal__layout modal__content")
= yield
Run Code Online (Sandbox Code Playgroud)
在我的application.html.haml,我有一个div定义模态位置如下:
-# ...
%div(id="modal-holder")
- unless params[:nojs]
= javascript_include_tag 'desktop'
-# ...
Run Code Online (Sandbox Code Playgroud)
这是我的问题来自哪里.我有一个book controller用户可以添加书籍的地方.我有一个多步注册,我的new行动必须是我的模式.当用户点击New book链接时,我正在加载一个新模式.
我的New book链接:
= link_to "New book", new_book_path, class: "c-link--navbar", data: { modal: true }
Run Code Online (Sandbox Code Playgroud)
CoffeeScript的(重新写入jQuery)的模式(此代码这篇文章):
$(function() {
var modal_holder_selector, modal_selector;
modal_holder_selector = '#modal-holder';
modal_selector = '.modal';
$(document).on('click', 'a[data-modal]', function() {
var location;
location = $(this).attr('href');
$.get(location, function(data) {
return $(modal_holder_selector).html(data).find(modal_selector).modal();
});
return false;
});
return $(document).on('ajax:success', 'form[data-modal]',
function(event, data, status, xhr) {
var url;
url = xhr.getResponseHeader('Location');
if (url) {
window.location = url;
} else {
$('.modal-backdrop').remove();
$(modal_holder_selector).html(data).find(modal_selector).modal();
}
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
现在我new action的yield那个将被加载到我的远程模态中:
= form_for(@book, remote: remote, html: {role: :form}, class: "c-form") do |f|
-# Forms here...
= f.submit "Add a new book", class: "c-btn"
Run Code Online (Sandbox Code Playgroud)
如果用户的信息正确,我的模态会将用户重定向到general action其他我希望保持在同一页面并闪烁错误消息,但问题是我没有使用它partial,它会自动将我重定向到新页面new action.
我的books_controller.rb:
class BooksController < ApplicationController
respond_to :html, :json, :js
...
def index
@books = current_user.books
end
def new
@book = current_user.books.build
respond_modal_with @book
end
def create
@book = current_user.books.build(book_params)
if @book.save
redirect_to general_book_path(@book), notice: "Saved."
else
flash.now[:notice] = "Something went wrong."
render :new
end
end
...
end
Run Code Online (Sandbox Code Playgroud)
模态响应者:
class ModalResponder < ActionController::Responder
cattr_accessor :modal_layout
self.modal_layout = 'modal'
def render(*args)
options = args.extract_options!
if request.xhr?
options.merge! layout: modal_layout
end
controller.render *args, options
end
def default_render(*args)
render(*args)
end
def redirect_to(options)
if request.xhr?
head :ok, location: controller.url_for(options)
else
controller.redirect_to(options)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我试着添加类似的东西
respond_to do |format|
format.html
format.json { render json: @book }
end
Run Code Online (Sandbox Code Playgroud)
我的create方法,但它不起作用,仍然将我重定向到新的页面.此外,我已经问了一个类似的问题,我注意到我的JavaScript代码在一个单独的页面上工作.只要我理解问题是我的远程模态操作页面.有人可以帮我弄清楚当用户输入错误的输入并打开我的javascript时如何留在同一页面上?
TBH 这是一个抽象的解决方案,与您的问题没有直接关系,但它可以工作(bootstrap+jq,基于生产代码)
咖啡脚本
$.showModal = (title, body) ->
$("#modal").find(".modal-title").html(title)
$("#modal").find(".modal-body").html(body)
$("#modal").modal("show")
$("#modal").find(".modal-submit").click () ->
$("#modal").find("form").submit()
Run Code Online (Sandbox Code Playgroud)
Html - 典型的引导模式
<div id="modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="<%= t :close %>" title="<%= t :close %>">
<span aria-hidden="true"></span>
</button>
</div>
<div class="modal-body">
<p></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal"><%= t :close %></button>
<button type="button" class="btn btn-success modal-submit"><%= t :save %></button>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
new.js.erb或其他 Rails 视图
$.showModal(
"<%= escape_javascript t :edit_comment %>",
"<%= escape_javascript(render 'form', :comment => @comment) %>"
);
Run Code Online (Sandbox Code Playgroud)
链接到打开模式
<%= link_to t(:new), [:new, comment], :remote => true, class: "btn" %>
Run Code Online (Sandbox Code Playgroud)
您可能想使用自己的 ajax 请求 - 在服务器错误/超时的情况下,该请求不会执行任何操作
| 归档时间: |
|
| 查看次数: |
1621 次 |
| 最近记录: |