Rails的远程链接模式的创建和编辑记录
问题描述:
我也跟着教程中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。我已经确定在萤火虫中没有错误。
我错过了什么?
答
您是否修改了控制器来呈现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
方法来显示模态。
请注意,j
是escape_javascript
的别名。 (少击键)
请确保您的div
的编号为modal-container
在index.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代码中的顶级位置在文档中避免影响模态的外观和/或功能的其它组件。
希望这会有所帮助!
你可以发布你的'index.html.haml'吗? –
@ArunKumar问题中的第一个代码段目前是我的完整索引文件。我删除了所有其他设备,试图让它工作 – Herm
添加了一个解决方案。让我知道它是否解决了您的问题。 –