Reputation: 12679
On Rails 3.2.3, I have a form in a partial view, using bootstrap 2.0.2 modals
#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();
});
});
I the "main" page I've a link button to call the partial :
%td= link_to "Tag", tagging_path(:id => "#{repo.id}", :tags_string => "#{repo.tags}"), "data-toggle" => "modal", :class => "tag add-tag btn btn-primary
and the controller has the relative tagging method.
Now I have two problems:
first
when I click the button it display the form correctly but it leaves the "main" page (which I don't want), displaing the relative href (which is empty), as a background :
http://localhost:3001/tagging?id=4f82cb8ef370ff0001000699&tags_string=
that is generated by the 'tagging_path' in the link button. I cannot just call the modal like in bootstrap example :
<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-large">Launch demo modal</a>
because I need to fill the controller/model with form params so ..., how can this be done ?
second:
when I click the "Add tag" button in the form, the database get the right content and everything works but the form doesn't disapear until I click "X" link, which is just a link to root_path and not a JS action.
so it seems js is not working properly
$(document).ready(function() {
$('#myModal .btn.btn-primary').click(function() {
$('#myModal').modal('hide');
$('form').submit();
});
});
UPDATE:
This is the generated 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>
...
Upvotes: 1
Views: 5719
Reputation: 14219
1) Use event.preventDefault()
to stop the link action
2) Your selector needed a space. Or you can just leave .btn
out entirely.
$(document).ready(function() {
$('#myModal .btn-primary').click(function(e) {
e.preventDefault();
$('#myModal').modal('hide');
$('form').submit();
});
});
If you post the rendered HTML markup of your modal I can help with your selector since using the class is generally not a good idea if you want to capture a specific button click.
Edit given markup:
Okay, you can just make your button a button
type instead of submit
and select it by its name
instead. If you use a submit
you don't need to call form.submit
, it does that by default. Using a button
type gives you more control.
$('input[name="commit"]').click(function() {
//$('form').submit();
$('#myModal').modal('hide');
});
Working example: http://jsfiddle.net/tcjCj/3/
Upvotes: 1