Jquery 可重用远程模式Rails
我正在尝试创建一个可重用的远程模式。其思想是创建一个模型容器,并使用Jquery 可重用远程模式Rails,jquery,ruby-on-rails,ajax,bootstrap-modal,haml,Jquery,Ruby On Rails,Ajax,Bootstrap Modal,Haml,我正在尝试创建一个可重用的远程模式。其思想是创建一个模型容器,并使用yield将内容渲染到其中,并在需要时渲染内容 注意:所有HTML代码都将用HAML编写。我用gem来处理情态动词 我的模态容器布局(非局部): 在我的application.html.haml中,我有一个div,它定义了模式位置,如下所示: -# ... %div(id="modal-holder") - unless params[:nojs] = javascript_include_tag 'desktop' -
yield
将内容渲染到其中,并在需要时渲染内容
注意:所有HTML
代码都将用HAML
编写。我用gem来处理情态动词
我的模态容器布局(非局部):
在我的application.html.haml
中,我有一个div,它定义了模式位置,如下所示:
-# ...
%div(id="modal-holder")
- unless params[:nojs]
= javascript_include_tag 'desktop'
-# ...
这就是我的问题所在。我有一个书籍控制器
,用户可以在其中添加书籍。我有一个多步骤注册,我的新操作必须是我的模式。当用户单击新书
链接时,我正在加载一个新模式
我的新书
链接:
= link_to "New book", new_book_path, class: "c-link--navbar", data: { modal: true }
模式的CoffeeScript(重新写入jQuery
)(此代码来自:
现在,我的新操作
将加载到远程模式中的产量
:
= form_for(@book, remote: remote, html: {role: :form}, class: "c-form") do |f|
-# Forms here...
= f.submit "Add a new book", class: "c-btn"
如果用户信息正确,我的模式会将用户重定向到常规操作
,否则我想停留在同一页面上并闪烁错误消息,但问题是我没有使用部分
,它会自动将我重定向到带有新操作
的新页面
我的书籍\u控制器.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
到我的
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()
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>
新建.js.erb或其他rails视图
$.showModal(
"<%= escape_javascript t :edit_comment %>",
"<%= escape_javascript(render 'form', :comment => @comment) %>"
);
$.showmodel(
"",
“@comment)%>”
);
链接到打开模式
<%= link_to t(:new), [:new, comment], :remote => true, class: "btn" %>
true,类别:“btn”%>
您可能希望使用自己的ajax请求—如果您希望页面不被重定向,那么在服务器错误/超时的情况下,此请求将不会起任何作用。只需返回不带标题的ajax响应:location@plonknimbuzz我想在
ajax:success
上重定向,并在ajax:error
时保持在同一页面上。。。我想我的ajax和控制器设置有问题。非常感谢您的回复:)您好。。我检查了你的github档案。这不是一个开源项目?如果你让我把你的项目交给你,我可能很容易通过调试找到解决方案,否则我可以得到更深入的研究look@FabrizioBertoglio您好,我将在未来一两个小时内创建一个新项目,并将在这里的评论中让您知道!非常感谢您的帮助和时间!我仍在调查你的SO职位。你永远得不到答案的原因是你写的不对。你的问题有太多的信息,从某个角度来看,它是非常广泛的。我的建议是试着把你的问题分解成不同的部分。例如,您有步骤1、2、3、4。步骤1、2、4之所以有效,是因为您调试了流程,所以问题出在步骤3。然后创建一个简单的项目,以简化的方式重新创建问题(步骤3)。问题依然存在?然后把它贴在上面。不是吗?这是一条你可以用来解决问题的线索非常感谢你的回答!非常感谢你的时间。将尝试实施您的解决方案。
$.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()
<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>
$.showModal(
"<%= escape_javascript t :edit_comment %>",
"<%= escape_javascript(render 'form', :comment => @comment) %>"
);
<%= link_to t(:new), [:new, comment], :remote => true, class: "btn" %>