在 Rails 3.2.3 上,我在部分视图中有一个表单,使用引导程序 2.0.2 模态
#myModal.modal
.modal-header
.close{"data-dismiss" => "modal"}= link_to "x", root_path
%h3 Add Tags
.modal-body
= form_tag '/tagging', :remote => true do
= text_field_tag 'tags_string', params[:tags_string]
= hidden_field_tag 'id', params[:id]
.modal-footer
= submit_tag "Add tag", :class => "btn btn-primary"
:javascript
$(document).ready(function() {
$('#myModal .btn.btn-primary').click(function() {
$('#myModal').modal('hide');
$('form').submit();
});
});
我的“主”页面我有一个链接按钮来调用部分:
%td= link_to "Tag", tagging_path(:id => "#{repo.id}", :tags_string => "#{repo.tags}"), "data-toggle" => "modal", :class => "tag add-tag btn btn-primary
并且控制器具有相对标记方法。
现在我有两个问题:
第一的
当我单击按钮时,它会正确显示表单,但它会离开“主”页面(我不想要),显示相对 href(为空)作为背景:
http://localhost:3001/tagging?id=4f82cb8ef370ff0001000699&tags_string=
由链接按钮中的 'tagging_path' 生成。我不能像在引导示例中那样调用模态:
<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-large">Launch demo modal</a>
因为我需要用表单参数填充控制器/模型所以......,这怎么做?
第二:
当我单击表单中的“添加标签”按钮时,数据库会获得正确的内容并且一切正常,但是在我单击“X”链接之前表单不会消失,这只是指向 root_path 的链接而不是 JS 操作。
所以看起来js不能正常工作
$(document).ready(function() { $('#myModal .btn.btn-primary').click(function() { $('#myModal').modal('hide'); $('form').submit(); }); });
更新:
这是生成的 html :
<div class='modal hide fade' id='myModal'>
<div class='modal-header'>
<div class='close' data-dismiss='modal'><a href="/">x</a></div>
<h3>Add Tags</h3>
</div>
<div class='modal-body'>
<form accept-charset="UTF-8" action="/tagging" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="vlZaCIRV58BfDZ0xjYsefSEVL1LSpiI5UL1tgzbm/8o=" /></div>
<input id="tags_string" name="tags_string" type="text" />
<input id="id" name="id" type="hidden" />
<div class='modal-footer'>
<input class="btn btn-primary" name="commit" type="submit" value="Add tag" />
</div>
</form>
</div>
</div>
<td>
<a href="/tagging?id=4f82cb5cf370ff000100003a&tags_string=ruby+web+framework" class="tag add-tag btn btn-primary" data-toggle="modal">Tag</a></td>
</tr>
<tr>
<td>
<div class='gravatar'><img alt="Assets.github.com%2fimages%2fgravatars%2fgravatar-orgs" height="26" src="https://secure.gravatar.com/avatar/b0f9595244db739302ccce55bfbfc5e5?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png" width="26" /></div>
</td>
<td>
<a href="https://github.com/h5bp/html5-boilerplate">h5bp/html5-boilerplate</a>
</td>
<td class='watchers span1'>12105</td>
<td class='forks span1'>1781</td>
<td>A professional front-end template that helps you build fast, robust, adaptable, and future-proof websites.</td>
<td>Sun, Jan 24 at 6:03pm</td>
<td>Sat, Apr 14 at 2:10pm</td>
...