【问题标题】:Rails remote link modal to create and edit records用于创建和编辑记录的 Rails 远程链接模式
【发布时间】:2016-08-16 21:44:32
【问题描述】:

我已经按照here找到的教程进行操作

我想要完成的是允许用户使用模式编辑索引页面中的记录。

在我的 index.haml 视图中,我有这个:

- @bars.each do |bar|
  = link_to "Edit", edit_bar_path(bar), remote: true, class: "btn btn-default"

#bar-modal.modal.fade

在 _edit.haml 中:

.modal-header
  %h3= "Editing #{@bar.foo}"
= render "form"

在edit.js.erb中

$("#bar-modal").html("<%= escape_javascript(render 'edit') %>")
$("#bar-modal").modal("show")

在_form.haml中

= bootstrap_form_for @bar, remote: true do |f|  
  = f.text_field :foo
  = f.button "Save"

我的控制器是标准 rails 生成的 CRUD 控制器。

由于某种原因,如果我单击链接,这不会显示模式。我让它以不同的方式工作,但随后它打开了一个“创建”表单,而不是一个编辑表单。

我正在使用 bootstrap 和 haml。我已确保 firebug 中没有错误。

我错过了什么?

【问题讨论】:

  • 你能发布你的index.html.haml吗?
  • @ArunKumar 问题中的第一个代码段目前是我的完整索引文件。我删除了所有其他内容以尝试使其正常工作
  • 添加了解决方案。让我知道它是否能解决您的问题。

标签: jquery ruby-on-rails twitter-bootstrap ujs


【解决方案1】:

您是否修改了控制器以呈现js 响应?

在控制器的edit 操作中,添加

def edit
  #find the record
  respond_to |format|
    format.js # this will render edit.js.haml
    # other formats
  end
end

你在你的edit.js.haml中混合了erbhaml。将其替换为

$('#modal-container').html('#{ j(render 'edit') }');
$('#bar-modal').modal("show");

当执行第一行代码时,#bar-modal 存在于我们的文档中。然后你就可以在它上面调用modal方法来显示模态了。

请注意,jescape_javascript 的别名。 (减少击键次数)

确保divindex.html.haml 中有ID 为modal-container。你的模态(在_edit.html.haml)应该有一个bar-modal的ID。

_form.html.haml 中的模态应该如下所示:

.modal.fade.in#bar-modal
  .modal-dialog
     .modal-content
       .modal-header
         %h3= "Editing #{@bar.foo}"
       .modal-body
         = render "form"

请注意,我们有一个 id 为 bar-modal 的模态框,它将被渲染到占位符 div #modal-container 中,它应该在 index.html.haml 中。应该是这样的。

%div{id: "modal-container"}
  <!-- this is where the modal will go in -->

- @bars.each do |bar|
  = link_to "Edit", edit_bar_path(bar), remote: true, class: "btn btn-default"

modal-container 添加到文档顶部。从引导程序的文档中,

模态标记放置

始终尝试将模态框的 HTML 代码放在文档中的顶级位置,以避免其他组件影响模态框的外观和/或功能。

希望这会有所帮助!

【讨论】:

  • 现在好像发生了什么事。单击链接时,我在 firebug 中看到 500 错误。所以试图找出原因......一旦解决,我会告诉你。感谢您的帮助!!!
  • 我摆脱了 500 错误。但是模态仍然没有显示。我可以在我的日志中看到它正在呈现我期望的视图文件,但模式未显示在浏览器中。
  • @Herm 在网络选项卡中,您是否在响应正文中看到modal html?你在github上有项目吗?如果是,请发布网址。我去看看
  • 我只在网络标签中看到“编辑”行。这是“xhr”类型。没有别的东西了。我在 BitBucket 中有这个项目,但不幸的是它是一个私人仓库。
  • @Herm,您是否将'#{ j(render 'edit') }' 传递给html 方法?我怀疑你的edit.js.haml 有问题。您能分享一下您尝试过的代码的要点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2015-05-26
  • 2014-10-22
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多