I have a bug in my app but I do not know where to look. When I refactor my forms with the simple_form format like:
<%= simple_form_for([#schedule.doctor, #schedule], :html => { :class => 'form-horizontal'}) do |f| %>
<%= f.label :start_time %>
<%= f.text_field :start_time %>
<%= f.label :day %>
<%= f.select :day, Date::DAYNAMES.zip((0..6).to_a) %>
<%= f.label :is_available %>
<%= f.check_box :is_available %>
<%= f.submit %>
<% end %>
This is the HTML code it generates (see the text in bold)
<form id="new_schedule" class="simple_form form-horizontal" novalidate="novalidate" method="post" action="/doctors/1/schedules" accept-charset="UTF-8">
**<div style="margin:0;padding:0;display:inline">**
<label class="time optional control-label" for="schedule_start_time">Start time</label>
<input id="schedule_start_time" class="ui-timepicker-input" type="text" size="30" name="schedule[start_time]" autocomplete="off">
<label class="integer optional control-label" for="schedule_day">Day</label>
<select id="schedule_day" name="schedule[day]">
<label class="boolean optional control-label" for="schedule_is_available">Is available</label>
<input type="hidden" value="0" name="schedule[is_available]">
<input id="schedule_is_available" type="checkbox" value="1" name="schedule[is_available]">
<input type="submit" value="Create Schedule" name="commit">
</form>
Any idea what could be causing this issue?
I'm using bootstrap-sass with simple_forms
Solution:
I need it to set config.default_wrapper = :bootstrap in the simple_form.rb file, and also make sure to use simple_forms tags.
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.use :input
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
config.wrappers :prepend, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |input|
input.wrapper :tag => 'div', :class => 'input-prepend' do |prepend|
prepend.use :input
end
input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
end
end
config.wrappers :append, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |input|
input.wrapper :tag => 'div', :class => 'input-append' do |append|
append.use :input
end
input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
end
end
# Wrappers for forms and inputs using the Twitter Bootstrap toolkit.
# Check the Bootstrap docs (http://twitter.github.com/bootstrap)
# to learn about the different styles for forms and inputs,
# buttons and other elements.
config.default_wrapper = :bootstrap
end
Related
I updated to Rails4, and am now getting "wrong number of arguments (3 for 2)".
.col-md-6.col-md-offset-3
= form_for(resource, :as => resource_name, url: session_path(resource_name), :html => { :class => "search-form home-search"}) do |f|
= f.email_field :email, :id => 'beta_form', :class => 'beta_form', :placeholder => 'Email'
= f.password_field :password, :id => 'beta_form', :class => 'beta_form', :placeholder => 'Password'
= f.submit "Sign In", :class => 'btn btn-primary btn-lg btn-block beta_submit center-block'
= render "devise/shared/links"
All set. For some reason, I had to delete gem 'meta-search' after upgrading.
I'm trying to create a user sign up form with Devise that will also allow for the creation of a new business associated with that user.
I have a business model setup and can't seem to save the business information to the database. Below is my code, I'm fairly new to rails so I apologize if I'm asking a question with an obvious answer.
new.html.erb (user)
<div class="content">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>Sign Up</h1>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<label>Name</label>
<%= f.text_field :username, :autofocus => true, :class => "form-control", :placeholder => "Full Name" %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, :class => "form-control", :placeholder => "Email" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, :class => "form-control", :placeholder => "Password" %>
<p class="help-block">Passwords must be a minimum of 8 characters.</p>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :class => "form-control", :placeholder => "Retype Password" %>
</div>
<!-- Business Infomation -->
<h2>Business Information</h2>
<%= f.fields_for :business do |b| %>
<div class="form-group">
<%= b.label :name %>
<%= b.text_field :name, :class => "form-control", :placeholder => "Business Name" %>
</div>
<div class="form-group">
<%= b.label :address %>
<%= b.text_field :address, :class => "form-control", :placeholder => "Address" %>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<%= b.label :city %>
<%= b.text_field :city, :class => "form-control", :placeholder => "City" %>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<%= b.label :state %>
<%= b.text_field :state, :class => "form-control", :placeholder => "State" %>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<%= b.label :zip %>
<%= b.text_field :zip, :class => "form-control", :placeholder => "ZIP" %>
</div>
</div>
</div>
<div class="form-group">
<%= b.label :country %>
<%= b.text_field :country, :class => "form-control", :placeholder => "Country" %>
</div>
<% end %>
<div class="well">
<%= f.submit "Sign Up", :class => "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
user.rb
class User
include Mongoid::Document
include Mongoid::Paperclip
rolify
include Mongoid::Timestamps
#embeds_many :businesses, :class_name => "Business"
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :business_ids,
:reset_password_sent_at, :reset_password_within, :address, :city, :state, :zip, :country, :phone, :business_attributes
has_one :businesses
accepts_nested_attributes_for :businesses
validates_format_of :email, :with=>email_regexp, :allow_blank => true, :message=> "Justin"
#intercom
attr_accessor :company_name
attr_accessible :company_name
## Database authenticatable
field :email, :type => String, :default => ""
field :encrypted_password, :type => String, :default => ""
## Recoverable
field :reset_password_token, :type => String
#field :reset_password_sent_at, :type => Time
field :reset_password_sent_at, :type => Time
## Rememberable
field :remember_created_at, :type => Time
#field :remember_created_at, :type => String
## Trackable
field :username, :type => String
field :sign_in_count, :type => Integer, :default => 0
#field :current_sign_in_at, :type => Time
#field :last_sign_in_at, :type => Time
field :current_sign_in_at, :type => Time
field :last_sign_in_at, :type => Time
field :current_sign_in_ip, :type => String
field :last_sign_in_ip, :type => String
field :first_name, :type => String
field :last_name, :type => String
#field :business_ids, :type => Array
field :address, :type => String
field :city, :type => String
field :state, :type => String
field :zip, :type => String
field :country, :type => String
field :phone, :type => String
# User Avatar
attr_accessible :avatar
has_mongoid_attached_file :avatar,
:styles => { :full => ["512x512>", :jpg], :medium => ["256x256>", :jpg] },
:convert_options => {:medium => "-background black -gravity center -extent 256x256"},
:default_url => "/assets/avatar-blank.png"
validates_attachment_size :avatar, :less_than => 5.megabytes
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
## Confirmable
# field :confirmation_token, :type => String
# field :confirmed_at, :type => Time
# field :confirmation_sent_at, :type => Time
# field :unconfirmed_email, :type => String # Only if using reconfirmable
## Lockable
# field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts
# field :unlock_token, :type => String # Only if unlock strategy is :email or :both
# field :locked_at, :type => Time
## Token authenticatable
# field :authentication_token, :type => String
after_create :create_business
def create_business
Business.create(business_id: self.id)
end
def assign_default_role(b)
# assign a default role if no role is assigned
# IF, invite token make user an editor for business
# ELSE, make the user owner of the business
self.add_role "owner", b
end
#Returns a businesses for a user. The return type is an array of Business models.
def businesses
Business.find(get_business_ids)
end
#returns the user business_ids (Array of Strings)
def get_business_ids
Business.find_roles(nil, self).map{|b| b.resource_id.to_s}.to_a
end
end
Try override Devise's registration controller:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
# save business here
end
end
And then tell devise to use customized controller:
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
I'm trying to integrate bootstrap 3 with simple_forms (from master).
Right now, I have the following:
config/initializers/simple_form.rb:
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
end
config.default_wrapper = :default
config.boolean_style = :nested
config.button_class = 'btn'
end
config/initializers/simple_form_bootstrap.rb:
SimpleForm.setup do |config|
config.input_class = 'form-control'
config.wrappers :bootstrap, tag: 'div', class: 'form-group', error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper tag: 'div', class: 'controls' do |ba|
ba.use :input
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :prepend, tag: 'div', class: "form-group", error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper tag: 'div', class: 'controls' do |input|
input.wrapper tag: 'div', class: 'input-prepend' do |prepend|
prepend.use :input
end
input.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
end
end
config.wrappers :append, tag: 'div', class: "form-group", error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper tag: 'div', class: 'controls' do |input|
input.wrapper tag: 'div', class: 'input-append' do |append|
append.use :input
end
input.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
end
end
config.default_wrapper = :bootstrap
end
app/views/devise/sessions/new.html.haml
%div.panel.panel-auth
%div.panel-heading
%h3.panel-title Sign in
%div.panel-body
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
.form-inputs
= f.input :email, :required => false, :autofocus => true
= f.input :password, :required => false
= f.input :remember_me, :as => :boolean if devise_mapping.rememberable?
.form-actions
= f.button :submit, "Sign in"
%hr
= render "devise/shared/links"
But the generated HTML is wrong. Well, it's right according to BS2, but wrong to BS3. Here it is:
<div class="form-group boolean optional user_remember_me">
<label class="boolean optional control-label" for="user_remember_me">
Remember me
</label>
<div class="controls">
<input name="user[remember_me]" type="hidden" value="0">
<label class="checkbox">
<input class="boolean optional form-control" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
</label>
</div>
</div>
But it actually should be something like:
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
It's probably possible to fix this hacking around wrappers, but I can't get it working.
Can someone give me some advice about that?
Cheers
Like you said, you can get it working with a custom wrapper:
config.wrappers :checkbox, tag: :div, class: "checkbox", error_class: "has-error" do |b|
# Form extensions
b.use :html5
# Form components
b.wrapper tag: :label do |ba|
ba.use :input
ba.use :label_text
end
b.use :hint, wrap_with: { tag: :p, class: "help-block" }
b.use :error, wrap_with: { tag: :span, class: "help-block text-danger" }
end
Which you then reference in your input:
= f.input :remember_me, :as => :boolean, :wrapper => :checkbox if devise_mapping.rememberable?
Note however this won't work for collection_check_boxes:
= f.input :roles, as: :check_boxes, wrapper: :checkbox, collection: #employee_roles, label: false
You could try hacking together a custom input for the latter case, but it's a bit messy. Maybe somebody else knows a better way... and perhaps simple_form will catch up with bootstrap 3 soon enough.
Next configuration works perfectly for me with bootstrap 3:
SimpleForm.setup do |config|
...
config.boolean_style = :inline
...
end
Simple custom wrapper
config.wrappers :inline_checkbox, :tag => 'div', :class => 'checkbox', :error_class => 'has-error' do |b|
b.use :html5
b.use :placeholder
b.use :label_input
end
Usage:
= f.input :remember_me, :label => t('user.remember_me'), :as => :boolean, :wrapper => :inline_checkbox
I found a really simple solution for Bootstrap 3 checkboxes. Assuming you already have the bootstrap 3 wrapper configured, all I had to do is add a custom input in app/inputs as checkbox_input.rb:
class CheckboxInput < SimpleForm::Inputs::BooleanInput
# this exists to make the default 'boolean' css class in the form-group to be 'checkbox' instead
end
and use it via:
= f.input :remember_me, :as => :checkbox if devise_mapping.rememberable?
This will change the css boolean on the form-group to be checkbox instead, which will get the proper styling.
Similarly, here is a version for radio-inline
class RadioInlineInput < SimpleForm::Inputs::CollectionRadioButtonsInput
# by default, this omits labels. You should use f.label before this to stick a label where you would like.
def initialize(builder, attribute_name, column, input_type, options = {})
super(builder, attribute_name, column, :radio_buttons, {label: false}.merge(options))
end
def item_wrapper_class
'radio-inline'
end
end
Used as:
= f.label :frequency
= f.input :frequency, collection: #membership_plan.interval_frequencies, as: :radio_inline
Here's a quick way to fix the checkbox issue whilst we wait for Rafael to implement Bootstrap3: https://github.com/wtfiwtz/simple_form_bootstrap3/commits/master
Make sure you add "config.bootstrap3 = true" to your initializer...
I have next requirements for my checkbox:
<div class="checkbox">
<input type="hidden" value="0" name="...">
<label>
<input type="checkbox" value="1" name="..."> Label text
</label>
</div>
Hidden input was extracted from label since it does not check checkbox on label click. I do not know why but I was not able to generate such html just using config so here is config + custom input class
# Config
config.wrappers :checkbox, tag: :div, class: "checkbox", error_class: "has-error" do |b|
# Form extensions
b.use :html5
b.use :placeholder
b.use :input
b.use :hint, wrap_with: { tag: :p, class: "help-block" }
b.use :error, wrap_with: { tag: :span, class: "help-block text-danger" }
end
# Input goes to app/inputs/inline_checkbox_input.rb
class InlineCheckboxInput < SimpleForm::Inputs::Base
def input
out = ''
out << #builder.hidden_field(attribute_name, value: 0).html_safe
out << "<label>"
out << #builder.check_box(attribute_name, {}, 1, nil)
out << "#{options[:label]}</label>"
out
end
end
# Usage
= f.input :name, :label => 'Label text', :wrapper => :checkbox, :as => :inline_checkbox
You can just use this:
<%= f.input :remember_me, as: :boolean, :label => false, :inline_label => "Remember me" if devise_mapping.rememberable? %>
Is there a way using configuration to make simple_form present in a different order it's components.
SimpleForm currently displays this way: label, input, errors, hint
I want: label, input, hint, errors
If you cannot manage it with css you might want bo built/edit a wrapper
For example, in the default simple_form wraper (/config/initializers/wrap_parameters.rb) I have switched the order of b.use :hint and b.use :error lines
config.wrappers :default, :class => :input,
:hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
...
## Inputs
b.use :label_input
b.use :error, :wrap_with => { :tag => :span, :class => :error }
b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
end
Is this correct to send an ajax request?
This code isn't working, what do I need to change here? Any better way to send an ajax form?
<%= form_tag item_create_path, :remote => true, :id => 'create_item' do %>
<p>
<b> <%= label_tag :"Name" %></b> <%= text_field_tag :name, nil, :maxlength => 40, :size => 70 %>
<b> <%= label_tag :"Date" %></b> <%= text_field_tag :date, nil, :maxlength => 10, :size => 10, :value => Time.now.utc.strftime("%Y-%m-%d") %>
<b> <%= label_tag :"Time" %></b> <%= text_field_tag :time, nil, :maxlength => 10, :size => 10, :value => Time.now.localtime.strftime("%H:%M:%S") %>
</p>
<p>
<b> <%= label_tag :Description %></b> <%= text_field_tag :description, nil, :maxlength => 50, :size => 50 %>
</p>
<%= hidden_field_tag :type, nil, :value => "new" %>
<p class="button"><%= submit_tag " Create ",:onclick=>"javascript:submitForm()" %></p>
<% end %>
function submitForm() {
$.ajax({type:'POST', url: '/item/create', data:$('#create_item').serialize(), success: function(response) {
$('#create_item').find('#item').html(response);
}});
return false;
}
Use this it will sure work
<%= form_for(:customer, :url => {:controller => "subscribers", :action => "change_password"}, :html => {:id => 'edit_password_form', :method => :get, :onsubmit => "return false;"}) do |f| %>
<%= hidden_field_tag "ids", :id => "ids"%>
<div class="ports-fields">
<div class="pass">
<label style="margin-top:4px;">Password</label>
<%= f.password_field :password, :class=> 'fields', :placeholder => 'Password' %>
</div>
<div class="pass">
<label>Confirm Password</label>
<%= f.password_field :password_confirmation, :class=> 'fields', :placeholder => 'Password Confirmation' %>
</div>
<div class="change-pass-btn-submit">
<%= submit_tag "CHANGE PASSWORD", :id => "edit_password_btn" %>
</div>
<!--modify ports ends-->
</div>
<% end %>
and jquery code for this
<script type="text/javascript">
$("#edit_port_btn").click(function() {
var container = $("#info");
$("#edit_port_form").submit(function() {
$(this).unbind('submit').ajaxSubmit({
success: function(data) {
container.html(data);
hudMsg('success', 'Modify ports successfully.');
}
})
});
});
</script>
And enjoyy....