Allow closing a modal when clicking outside - twitter-bootstrap-3

I used bootstrap-modal plugin for my project. But now I have an issue with closing my modal by clicking outside of it. I tried to pass the options like: data-backdrop="true", but it doesn't work. Any ideas of what's going wrong?
EDITED:
My modal is in a partial (using Rails)
<div id="editModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<div class="text-center">
<div class="btn-group topbar header-buttons" role="group" aria-label="...">
<%= link_to 'Add', '#addModal', { 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-dismiss' => 'modal' } %>
<%= link_to 'Edit', '#', class: 'btn btn-default disabled' %>
<%= link_to 'Delete', '#deleteModal', { 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-dismiss' => 'modal' } %>
</div>
</div>
</div>
<div class="modal-body">
<%= form_for (#change_office_address), remote: true, format: :json, html: { class: :contact_form } do |f| %>
<div id="error_explanation" style='display:none;' class="bg-danger text-danger alert fade in alert-danger alert-dismissable errors">
<ul>
<% if #change_office_address.errors.any? %>
<% #change_office_address.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
</ul>
</div>
<%= f.text_field :name, placeholder: 'Name', class: 'form-control' %>
<br>
<%= f.text_field :email, placeholder: 'e-mail', class: 'form-control' %> <br>
<%= f.label :city_id %>
<%= f.collection_select :city_id, City.order(:name), :id, :name,
{ include_blank: true }, { class: 'form-control' } %>
<br>
<%= f.label :insurer_id %>
<%= f.collection_select :insurer_id, Insurer.order(:short_name), :id, :short_name,
{ include_blank: true }, { class: 'form-control' } %>
<br>
<%= f.label :office_id %>
<%= f.collection_select :office_id, Office.order(:name), :id, :name,
{ include_blank: true }, { class: 'form-control' } %>
<br>
<%= f.text_area :edit_office_address, placeholder: 'New address', class: 'form-control', cols: '30',
rows: '5' %> <br>
<div class="text-center">
<%= f.submit, class: 'btn btn-default' %>
</div>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
I call a modal by: <%= link_to 'Edit', '#editModal', {'data-toggle' => 'modal'} %>
Thanks ahead!

I added this code, and now it's working:
$("#myModal").click(function(ev){
if(ev.target != this) return;
$('#myModal').modal('hide');
});

Backdrop option is true by default.
Check: http://getbootstrap.com/javascript/#modals-options
Make sure if you're call jquery.min.js and bootstrap.min.js correctly.
To test, press the Esc key and see if the modal is closed. By default, the keyboard option is also true.
If backdrop and keyboard doesn't working, there is a great chance you've called the .js wrongly.
Remember: call jquery.js BEFORE bootstrap.js.
If necessary, comment your console log here.
Finally, remember that Bootstrap does not support multiple modals.

Related

Why can I create a model with an hstore attribute, but get a 500 error when I try to update?

Borrowing heavily from:
https://github.com/heroku/hstore_example
I can define any number of "params" (my hstore column) from the New view when creating a new model and the hstore column gets set properly and the model is saved. Perfect!!
However, when I try to edit and submit I'm getting:
expected Hash (got String) for param `id'
Where 'id' is the name I gave to the hstore key of the "param" that I created.
Also interesting...I can Create AND Update the SAME MODEL from the console no problem.
WTF?!?
Here's my model:
class Testmethod < ActiveRecord::Base
attr_accessible :name, :params
serialize :params, ActiveRecord::Coders::Hstore
def as_json(options = {})
{ :name => name, :params => params }
end
end
Here's the form:
<%= form_for(#testmethod) do |f| %>
<% if #testmethod.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#testmethod.errors.count, "error") %> prohibited this testmethod from being saved:</h2>
<ul>
<% #testmethod.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :script %>
<%= f.text_area :script, :rows => 6 %>
</div>
<h4>Parameters</h4>
<div class="well">
<div class='clearfix attributeContainer'>
<div class='row'>
<p class='span3 underline'>Name</p>
<p class='span3 underline'>Value</p>
</div>
<% #testmethod.params.try(:each) do |key, value| %>
<div class="row">
<p class='span3'>
<%= text_field_tag key, key, :class => 'text_field dynamicAttributeName' %>
</p>
<p class='span3'>
<%= text_field key, :class => 'text_field', :value => value %>
</p>
<p class='span1'>
<a herf='#' class='btn removeRow'>X</a>
</p>
</div>
<% end %>
<div class='row attributeTemplate <%= 'hide' if #testmethod.params.present? %>'>
<p class='span3'>
<input class='text_field dynamicAttributeName' id='' name='' placeholder='New Attribute name' size='30' type='text' />
</p>
<p class='span3'><input class='text_field' id='bar' name='' placeholder='value' size='30' type='text' /></p>
<p class='span1'><a herf='#' class='btn removeRow'>X</a></p>
</div>
<%= link_to 'Add Parameter', '#', :class => 'btn addAttribute btn-mini' %>
</div>
</div>
<div class="row">
<div class="span12">
<div class="actions">
<%= f.submit "Save", :class => 'btn btn-primary ' %> or <%= link_to "Cancel", request.referrer || testmethods_path %>
</div>
</div>
</div>
<% end %>
<%= content_for :js do %>
<script type='text/javascript'>
$('.attributeContainer').delegate('.dynamicAttributeName', 'keyup', function(event){
nameElem = $(this);
valueElem = nameElem.closest('.row').children('p').children('.text_field')
value = nameElem.val().toLowerCase();
valueElem.attr('id', 'testmethod_params_' + value );
valueElem.attr('name', 'testmethod[params][' + value + ']');
valueElem.attr('placeholder', 'value for ' + value );
})
$('.attributeContainer').delegate('.removeRow', 'click', function(){
console.log($(this).closest('.row'))
$(this).closest('.row').html('');
})
$('.addAttribute').on('click', function(){
contents = "<div class='row'>" + $('.attributeTemplate').html() + '</div>';
$(this).before(contents);
})
</script>
<% end %>

Rails two related models in one form not saving

My form fields are:
<%= form_tag signup_path, :class=>"no-ajax form-signin" do %>
<fieldset>
<legend>Company</legend>
<div class="field">
<label>Company Name</label>
<%= text_field :company, :name, :class => "input-block-level" %>
</div>
<%= hidden_field :company, :accounttype_id, :value => 2 %>
</fieldset>
<fieldset>
<legend>User</legend>
<div class="field">
<label>First Name</label>
<%= text_field :user, :first_name, :class => "input-block-level" %>
</div>
<div class="field">
<label>Last Name</label>
<%= text_field :user, :last_name, :class => "input-block-level" %>
</div>
<div class="field">
<label>Email</label>
<%= text_field :user, :email, :class => "input-block-level" %>
</div>
<div class="field">
<label>Role</label>
<%= text_field :user, :role_id, :value => 2 %>
</div>
</fieldset>
<fieldset>
<div class="field">
<label>Password</label>
<%= text_field :user, :password, :class => "input-block-level" %>
</div>
<div class="field">
<label>Confirm Password</label>
<%= text_field :user, :password_confirmation, :class => "input-block-level" %>
</div>
</fieldset>
<div class="form-actions">
<%= submit_tag 'Sign Up', :class => "btn btn-large btn-success btn-block" %>
</div>
<% end %>
My controller for the form is:
def signup
#company = Company.new
#user = #company.users.build
if #company.save
redirect_to :action => 'success'
else
render :action => 'signup'
end
end
When I save, it says that it fails.
This is the data that was posted (pasted from the rails console):
{"utf8"=>"✓", "authenticity_token"=>"9y4JeSvm34P05FBKbP3D3aToHlwejFZBkSyPbmGMJrk=", "company"=>{"name"=>"Test Co", "accounttype_id"=>"2"}, "user"=>{"first_name"=>"Andy", "last_name"=>"Bernard", "email"=>"andy#testco.com", "role_id"=>"2", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
What is the form unable to save
is there a better way to do this process? (create new Company, then create user linked to that company, all in one form)
The signup action in your controller does not save #company because no attributes were passed when Company.new was called.
#company = Company.new(params[:company])
will pass the parameters "company"=>{"name"=>"Test Co", "accounttype_id"=>"2"}.
Likewise for "user"=>{...}:
#user = #company.users.build(params[:user])
For an all-in-one form, Rails provides the following method accepts_nested_attributes_for which allows you to create both the company and the user in one go.

PUT routes broken by scope

In my application, I have several resources scoped into 'admin' as follows (in routes.rb):
scope 'admin', :as => 'admin' do
resources :events
end
So when I go in to use the #new or #update methods through the form, I end up getting the ActionController exception saying No route matches [PUT] "/admin/events".
rake routes produces the following:
admin_events GET /admin/events(.:format) events#index
POST /admin/events(.:format) events#create
new_admin_event GET /admin/events/new(.:format) events#new
edit_admin_event GET /admin/events/:id/edit(.:format) events#edit
admin_event GET /admin/events/:id(.:format) events#show
PUT /admin/events/:id(.:format) events#update
DELETE /admin/events/:id(.:format) events#destroy
_form code:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<%= form_for #event, :html => { :class => 'form-horizontal' }, :url => url_for(:controller => 'events', :action => 'index') do |f| %>
<fieldset>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group">
<%= f.label :event_date, :class => 'control-label' %>
<div class="controls date-selects">
<%= f.datetime_select :event_date, :start_year => 2012 %>
</div>
</div>
<div class="control-group">
<%= f.label :publish_date, :class => 'control-label' %>
<div class="controls date-selects">
<%= f.datetime_select :publish_date, :start_year => 2012 %>
</div>
</div>
<div class="control-group">
<%= f.label :blurb, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :blurb, :class => 'span6 wysi' %>
</div>
</div>
<div class="control-group">
<%= f.label :graphic, :class => 'control-label' %>
<div class="controls">
<%= f.file_field :graphic %>
</div>
</div>
<div class="control-group">
<%= f.label :tix_link, :class => 'control-label' %>
<div class="controls">
<%= f.url_field :tix_link %>
</div>
</div>
<div class="form-actions">
<%= f.submit 'Submit', :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")), admin_events_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
What am I doing wrong?
Try changing your form tag from:
<%= form_for #event, :html => { :class => 'form-horizontal' }, :url => url_for(:controller => 'events', :action => 'index') do |f| %>
to:
<%= form_for [:admin, #event], :html => { :class => 'form-horizontal' } do |f| %>
This will use the proper admin scope and properly generate the url, depending if the #event is persisted on the database or not.

Capybara::ElementNotFound:

I am testing my Rails app's sign up form with RSpec and Capybara. When I want to test it I face this error,
Failures:
1) user registration allows new users to register with an email address and password
Failure/Error: fill_in "Confirmation", :with => "foo"
Capybara::ElementNotFound:
no text field, text area or password field with id, name, or label 'Confirmation' found
# ./user_spec.rb:9:in `block (2 levels) in <top (required)>'
This is my form which is a sign up form,
<div id="box">
<div class="block" id="block-signup">
<h2>Sign up</h2>
<div class="content">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => "form login"}) do |f| %>
<% if #user.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<%end%>
<div class="group wat-cf">
<div class="left">
<label class="label"><%= f.label :email %></label>
</div>
<div class="right">
<%= f.email_field :email %>
<span class="description">Ex: test#example.com</span>
</div>
</div>
<div class="group wat-cf">
<div class="left">
<label class="label"><%= f.label :password %></label>
</div>
<div class="right">
<%= f.password_field :password %>
<span class="description">Must contains the word 'yeah'</span>
</div>
</div>
<div class="group wat-cf">
<div class="left">
<label class="label"><%= f.label :confirmation %></label>
</div>
<div class="right">
<%= f.password_field :password_confirmation %>
<span class="description">Don't miss any letters</span>
</div>
</div>
<div class="group navform wat-cf">
<button class="button" type="submit">
<%= f.submit "Sign in", :type => :image, :src => "http://pilu.github.com/web-app-theme/images/icons/tick.png" %>
</button>
</div>
<% end %>
</div>
<div id="links-sup">
<h4><%= render "links" %></h4></div>
</div>
</div>
And my test file is spec/requests/user_spec.rb
require 'spec_helper'
describe "user registration" do
it "allows new users to register with an email address and password" do
visit "/users/sign_up"
fill_in "Email", :with => "user#eample.com "
fill_in "Password", :with => "foo"
fill_in "Confirmation", :with => "foo"
click_button "Sign up"
page.should have_content("Welcome! You have signed up successfully.")
end
end
You have any idea to solve it?
Well, the error here indicates that it cannot find textbox with label "Confirmation". Here you have problem with html. There is no textbox with Confirmation label. You can test by clicking that label "Confirmation", the cursor won't be in the password_confirmation textbox.
See the difference why click on "Password" label, the cursor is in the password textbox. That's why there is no error on password field.
You could change the markup a little bit to f.label :password_confirmation.
When you see the error Capybara::ElementNotFound, it simply means it cannot find that element you reference. Therefore you need to check your markup is correct.

Advices to clean this show.html.erb view (Rails)?

The following view shows a single post and its comments:
views/posts/show.html.erb:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<div class="post-<%= #post.id %>">
<h3><span class="vote-count"><%= #post.total_votes %></span> votes</h3><br />
<div class='voted-user'>
<% #post.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
<%= link_to "Vote Up", vote_up_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-up" %><br />
<%= link_to "Vote Down", vote_down_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-down" %><br />
<h2>Comments</h2>
<p><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></p>
<p><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></p>
<% #comments.map do |comment| %>
<div class="comment-<%= comment.id %>">
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Vote:</b>
<span class="vote-count"><%= comment.total_votes %></span>
<div class='voted-user'>
<% comment.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<p>
<b>Link</b>
<%= link_to "Show Post Comment", [#post, comment] %>
</p>
<p>
<b>Vote</b>
<%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
</p>
</div>
<% end %>
<%= will_paginate #comments %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if current_user.id == #post.user_id %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
I just focused on making things work so I totally forgot to make it clean.
I'm a Rails beginner and I would like some suggestions or advises to clean this view
(if you suggest to move code to another file please mention the name of the file and the directoy). Thanks in advance.
Good on you for wanting to clean it up. This is some of what I would do. I've included some examples of a few things here: Partials, Helpers, and also cleaned up the HTML a little to allow more control over the style in your stylesheets (which I left out, but you can figure out that part I'm sure). If this was my project, I would extract everything even more (for example, I would probably move the entre "edit/back" links at the bottom, as well as the "Order" links, to their own partials or helpers, since I would probably be using them in a lot of places throughout the application.) And if your comments are polymorphic (i.e., not just for posts), then definitely move that into its own view file. Also, I did this a rather quickly, so it might have some errors, sorry if you find any.
_post.html.erb
<article class="post-info">
<span class="title"><b>Title:</b> <%= post.title %></span>
<p><%= post.content %></p>
<span class="user"><b>User:</b> <%= post.user.username %></span>
</article>
<div class="post" id="post-<%=post.id%>">
<div class="vote-count"><%= post.total_votes %></span> votes
<ul class="voted-user">
<% post.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
<ul class="voting">
<li><%= link_to "Vote Up", vote_up_path(post, :votable_type => "Post"), :remote => true, :class => "vote-up" %></li>
<li><%= link_to "Vote Down", vote_down_path(post, :votable_type => "Post"), :remote => true, :class => "vote-down" %></li>
</ul>
_comment.html.erb
<div class="comment" id="comment-<%=comment.id%>">
<article class="comment">
<b>Comment:</b> <%= comment.content %>
</article>
<div class="votes-total">
<b>Votes:</b> <span class="vote-count"><%= comment.total_votes %></span>
<ul class='voted-user'>
<% comment.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
</div>
<div class="commenter">
<b>Commenter:</b> <%= link_to comment.user.username, comment.user %>
</div>
<div class="link">
<b>Link:</b> <%= link_to "Show Post Comment", [comment.post, comment] %>
</div>
<div class="vote">
<b>Vote:</b> <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %>
</div>
</div> <!-- .comment -->
show.html.erb
<h2>Posts</h2>
<%= render #post %>
<h2>Comments</h2>
<ul class="order">
<li><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></li>
<li><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></li>
</ul>
<%= render #comments %>
<%= will_paginate #comments %>
<h2>Add a Comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<ul class="manage">
<li><%= edit_link_if_allowed(current_user, #post) %></li>
<li><%= link_to 'Back', posts_path %></li>
</ul>
Posts Helper
def edit_link_if_allowed(current_user, post)
link_to "Edit", edit_post_path(post) if post.user_id == current_user.id
end
You could use Partials