2016-08-16 119 views
1

我也跟着教程中hereRails的远程链接模式的创建和编辑记录

我试图完成,是让用户从索引页编辑记录,采用模态。

在我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" 

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

由于某种原因,如果点击链接,则不会显示模式。我以不同的方式开展工作,但之后它开辟了一个“创造”形式,而不是编辑形式。

我正在使用bootstrap和haml。我已经确定在萤火虫中没有错误。

我错过了什么?

+0

你可以发布你的'index.html.haml'吗? –

+0

@ArunKumar问题中的第一个代码段目前是我的完整索引文件。我删除了所有其他设备,试图让它工作 – Herm

+0

添加了一个解决方案。让我知道它是否解决了您的问题。 –

回答

2

您是否修改了控制器来呈现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 haml混合erb。更换与

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

当执行的第一行代码,#bar-modal存在我们的文件内。然后你可以调用modal方法来显示模态。

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

请确保您的div的编号为modal-containerindex.html.haml。而你的模式(在_edit.html.haml)应该有一个ID为bar-modal

而且在_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代码中的顶级位置在文档中避免影响模态的外观和/或功能的其它组件。

希望这会有所帮助!

+0

它看起来像现在正在发生的事情。点击链接时,我发现Firebug中有500个错误。所以试图弄清楚为什么...我会让你知道一旦解决。谢谢您的帮助!!! – Herm

+0

我摆脱了500错误。但该模式仍未显示。我可以在我的日志中看到它正在渲染我期望的视图文件,但模式不会显示在浏览器中。 – Herm

+0

@Herm在网络选项卡中,您是否在响应正文中看到'modal html'?你在github上有这个项目吗?如果是,发布网址。我会看看 –