I'm trying to style checkboxes in my rails app.
<div class="form-group">
<%= f.label :tag_list %>
<p>Science</p>
<%= f.check_box :tag_list, { :multiple => true }, 'science', nil %>
<p>Math</p>
<%= f.check_box :tag_list, { :multiple => true }, 'math', nil %>
</div>
In my application.js
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery-ui/button
//= require jquery-ui/datepicker
//= require jquery-ui/slider
//= require jquery-ui/spinner
//= require jquery-ui/tooltip
//= require jquery-ui/effect
//= require flatuipro
//= require flat-ui.min
//= require sweet-alert-confirm
//= require turbolinks
//= require_tree .
$(':checkbox').radiocheck();
I'm using something called flat-ui and am trying to apply the styles they provide for check boxes. How do I do this?
Looking at the documentation, it seems like the class checkbox needs to be given to the label, and the input needs to be inside the label. Your markup would then look something like this:
<div class="form-group">
<%= f.label :tag_list, class: 'checkbox' do %>
<p>Science</p>
<%= f.check_box :tag_list, { multiple: true }, 'science', nil %>
<% end %>
</div>
Then, make sure you are calling $(':checkbox').radiocheck(); in your JavaScript, and that you have included the Flat-UI js file.
Update from chat:
Make sure that each input has its own label:
<div class="form-group">
<%= f.label :tag_list, class: 'checkbox' do %>
<p>Science</p>
<%= f.check_box :tag_list, { multiple: true }, 'science', nil %>
<% end %>
<%= f.label :tag_list, class: 'checkbox' do %>
<p>Math</p>
<%= f.check_box :tag_list, { multiple: true }, 'math', nil %>
<% end %>
...
</div>
Also make sure that the JavaScript isn't called until the page has finished loading:
// application.js
$(function() {
$(':checkbox').radiocheck();
});
You need the radiocheck js file:
You can download the js file: radiocheck.js
Include radiocheck.js file in app/assets/javascripts folder.
Add this line in your application.js and the file will look like this:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require flat-ui.min
//= require radiocheck
//= require_tree .
$(function(){
$(':checkbox').radiocheck();
});
Related
I am trying to use parsley-rails validation on some forms in my rails app. Problem is that it works perfectly on some forms and doesn't submit on other forms.
I've tried adding required true to all form elements. I have also used different variations of "data-parsley-validate" => true, "data-parsley-validate" => "". Nothing seems to work. Here is the code to the form that works.
= simple_form_for(([#place, #place.reviews.build]), html: {"data-parsley-validate" => true}) do |f|
%p= "Please rate your experience"
.rating-form
%hr
= f.input :comment, input_html: {maxlength: 1000, rows: 10}, label: "Please describe your experience at the location", hint: "Enter a brief description of your experience", error: "You must provide a description for this review.", required: true
%hr
= f.submit "Review this place", :class => "btn btn-primary btn-large"
:javascript
$('.rating-form').raty({
path: '/assets',
scoreName: 'review[rating]'
});
However, I'm unable to submit this form after validation:
= simple_form_for(#place, html: {class: "form-group", "data-parsley-validate" => true}) do |f|
= f.error_notification
.form-inputs
= f.input :name, label: "Please enter the name of your place", hint: "eg. Papa Chris's Pizza.", required: true
= f.label :Select_a_category
%br
= f.collection_select :category_id, Category.all, :id, :name, prompt: "Please choose a category", required: true
%br
= f.input :short_description, label: "Please enter a short description of your place", hint: "eg. Papa Chris's Pizza.", required: true
= f.input :description, as: :trix_editor, input_html: {maxlength: 1000, rows: 10}, label: "Please describe your place", hint: "Enter a brief description of your place", required: true
= f.attachment_field :place_images_files, direct: true, multiple: true
= f.simple_fields_for :locations do |location|
= location.input :address, label: "Please enter the address of your place", hint: "eg. Lumumba Drive", required: true
= location.input :city, label: "Please enter the city of your promotion", hint: "eg. Tema", required: true
= f.input :phone, label: "Please enter your phone number", hint: "eg. 030 412 3123", :input_html => { :maxlength => 10, :"data-mask" => "999 999 9999" }, required: true
= f.input :mobile_number,label: "Please enter your mobile number", hint: "eg. 020 111 2222", :input_html => { :maxlength => 10, :"data-mask" => "999 999 9999" }, required: true
= f.input :website,label: "Please enter your website", hint: "eg. papajohns.com"
.form-actions
= f.button :submit, class: "btn btn-large btn-primary"
Here is my application.js file:
//= require rails-ujs
//= require jquery3
//= require jquery-ui
//= require ./jquery.raty.js
//= require jquery.timepicker.js
//= require jquery-mask-plugin
//= require jquery.slick
//= require parsley
//= require refile
//= require trix
//= require popper
//= require bootstrap-sprockets
//= require leaflet
//= require dropzone
//= require underscore
//= require masonry/jquery.masonry
//= require_tree .
My application.css
*= require leaflet
*= require 'masonry/transitions'
*= require_tree
*= require_self
*= require trix
*= require jquery-ui
*= require jquery.timepicker.css
*= require dropzone/dropzone
*= require parsley
*/
Nothing shows in the console and web inspector. The submit button just won't work.
Apparently Trix is triggering parsley validation on a disabled input. See this Trix issue. The solution is to add
'data-parsley-excluded="[disabled]"
to your form, like this:
<form data-parsley-validate data-parsley-excluded="[disabled]">
<trix-editor></trix-editor>
<input type="submit">
</form>
i recendly add a gem tinymce and i did all the step that the page tell me but the result was a loooong string and i really don t know why, here i put my codes:
i create a tinymce.yml file with this:
default:
plugins:
- image
- link
alternate:
selector: textarea.table-editor
toolbar: styleselect | bold italic | undo redo | table
plugins:
- table
then i add the following sentence in applications.js:
// about supported directives.
//
//= require jquery3
//= require popper
//= require rails-ujs
//= require turbolinks
//= require jquery3
//= require popper
//= require bootstrap
//= require jquery.validate
//= require.validate.additional-methods
//= require tinymce
//= require turbolinks
//= require_tree .
S(document).ready(function(){
S('#new_user').validate({
rules {
'user[email]': {
required:true,
email:true,
},
'user[password]': {
required:tree,
minlength:1
},
'user[password_confirmation]': {
required:true,
equalTo: '#user_password',
}
},
})
})
and in the view i put the following:
<div class="field">
<%= form.label :content %>
<%= form.text_area :content, id: :publication_content, :rows => 10, :cols => 10,
:class => "tinymce" %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<%= tinymce %>
<% end %>
in all the case here is my database:
class CreatePublications < ActiveRecord::Migration[5.1]
def change
create_table :publications do |t|
t.string :title
t.references :city, foreign_key: true
t.string :content
t.timestamps
end
end
end
and this is what is showing me the page instead of the link and the imagen:
so i really don t know what can i do.
You have to call .html_safe on strings with html tags that you want to be HTML tags on the rendered view, otherwise ActionView scapes the tags and the browser shows them as text. You are not showing the view with the problem though, but a .html_safe on both lines should work.
I'm trying to use bootstrap-switch-rails on my Rails project, but I can't make it work. I've been mostly following this tutorial.
This is what I tried so far.
Added this in my Gemfile
gem 'bootstrap-switch-rails', '~> 3.0.0'
Added this in the top of my app/assets/stylesheets/custom.scss
#import "bootstrap";
#import "bootstrap3-switch";
#import "bootstrap-sprockets";
/* Rest of the file omitted */
This is my application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require bootstrap-switch
//= require_tree .
My application.css
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
*= require bootstrap-switch3
And finally, the form where I want to use the switch:
<% content_for :head do %>
<script type="text/javascript">
$(function() {
$("input:checkbox").bootstrapSwitch();
});
</script>
<% end %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#consultant) do |f| %>
<%= render 'consultants/consultant_error_messages' %>
<%= f.label :identifier %>
<%= f.text_field :identifier, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :disabled, "Habilitar/Deshabilitar" %>
<%= f.check_box :disabled, :data => {
:size=>'small', 'on-color'=>'success', 'on-text'=>'Habilitar', 'off-text'=>'Deshabilitar'
} %>
<%= f.submit "Guardar", class: "btn btn-primary" %>
<% end %>
</div>
</div>
The checkbox is being shown as a regular checkbox, instead of a switch.
Any ideas on what should I be missing?
Thanks in advance.
Try to replace require bootstrap-switch3 with require bootstrap3-switch in your application.css
Also try executing $("input:checkbox").bootstrapSwitch(); in the console to see the output.
Another user solved the issue writing the HTML instead of using the one generated by Rails: rails 4 : change state issue with Bootstrap Switch
I've been doing a project using the foundation-rails gem and now that I've got to a part where I need to use its javascript - a reveal modal, it doesn't seem to be working, I can't get any of the foundation javascript features to work and have tried for hours to no avail.
app/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require foundation.min
//= require modernizr.custom.72741
//= require fastclick
//= require foundation.reveal
//= require_tree .
$(document).foundation();
relevant parts of application.html.haml
%head
%meta{charset:"utf-8"}
%meta{name:"viewport", content:"width=device-width, initial-scale=1.0"}
%title Instaharam
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag "vendor/modernizr"
= csrf_meta_tags
%link{href:"http://fonts.googleapis.com/css?family=Dancing+Script", rel:"stylesheet", type:"text/css"}
%body
...
= javascript_include_tag 'application'
mark-up to display the modal
%li= link_to "Add a post", '#', :'data-reveal-id' => 'add-post-modal'
#add-post-modal.reveal-modal
- #new_post = Post.new
= form_for #new_post, data: {abide: true}, :html => {:multipart => true } do |f|
= f.label :image
= f.file_field :image, required: true
= f.label :caption
= f.text_field :caption
= f.submit name: "Create Post"
%a.close-reveal-modal& ×
When I click the link it simply takes me to the top of the page.
I'm customizing a simple_form gem-enabled form for which I'd like the phone number field to have a datamask from http://jasny.github.com/bootstrap/javascript.html#inputmask
My original code is:
<%= f.input :phone, :required => true,
:input_html => { :maxlength => 14} %>
What is the code required to pass something like into the f.input?
Using something like input_html and label_html doesn't seem to work.
in your form try this:
<%= f.input :phone, :required => true,
:input_html => { :maxlength => 14, :"data-mask" => "999-999-9999" } %>
Also, assuming you're running Rails 3.1+ and you have the jasny files in your vendor/assets/javascripts directory, be sure to include the bootstrap-inputmask file in app/assets/javascripts/application.js:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require bootstrap-inputmask
//= require_tree .