Rails form Images - ruby-on-rails

This is my form :
<%= form_for #user, validate: true do |f| %>
<div class="well-lg">
<h4>Personal Details</h4>
<div class="form-group">
<%= f.label :Role %> <br />
<%= f.select :role, options_for_role, {}, prompt: 'Select One',:class=> 'form-control' %>
</div>
<div class="form-group">
<%= f.label :First_Name %><br />
<%= f.text_field :fname, :class=> 'form-control' %>
</div>
<div class="form-group">
<%= f.label :Last_Name %><br />
<%= f.text_field :lname, :class=> 'form-control' %>
</div>
<div class="form-group">
<%= f.label :Email_Address %><br />
<%= f.email_field :email, :class=> 'form-control' %>
</div>
<div class="form-group">
<%= f.label :Upload_Image %><br />
<%= f.file_field :orgimg, :class => 'fileupload' %>
<div id = 'dvPreview>
</div>
</div>
</div>
<% end %>
Everything works fine but I wanted to preview the image which is uploaded in the form so how can I add image_tag with file field or is there any other solution??
Thanks !!!

By using the following code , I was able to preview the image while uploading
<script language="javascript" type="text/javascript">
$(function () {
$(".fileupload").change(function () {
if (typeof (FileReader) != "undefined") {
var dvPreview = $("#dvPreview");
dvPreview.html("");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
$($(this)[0].files).each(function () {
var file = $(this);
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
var img = $("<img />");
img.attr("style", "height:100px;width: 100px");
img.attr("src", e.target.result);
dvPreview.append(img);
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
dvPreview.html("");
return false;
}
});
} else {
alert("This browser does not support HTML5 FileReader.");
}
});
});

Related

ruby refresh concatenate field

Still not find a solution..I want that the filed fullname is automatically updated when I fill fields prénom and nom.
these fields are used in the sign_up registration from devise. (I 've done changes in devise controller to be able to use nom and prenom as fields.
I've done this code but seems that my function do nothing and fullname field stays empty.
I don't want to transform my code to html (I d like to use <% %> in my code...
<div class= "col-md-4 col-md-offset-4">
<h2 class="text-center"> Sign up</h2>
<br/>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
< <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$('user.prenom, user.nom').on('user', function(e) {
var changedFullName = $('user.prenom').val() + " " + $('user.nom').val()
$('user.fullname').val(changedFullName);
});
});
</script>
<div class="form-group">
<%= f.label :prenom %><br />
<%= f.text_field :prenom, autofocus: true, placeholder: "Prenom", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :nom %><br />
<%= f.text_field :nom, autofocus: true, placeholder: "Nom", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :fullname %><br />
<%= f.text_field :fullname, autofocus: false, placeholder: "Nom complet", class: "form-control", :readonly => true %>
</div>
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "Password", class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
thanks for your help
You should be using classes or ids to assign a jquery event. Here's my solution, based on your code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class= "col-md-4 col-md-offset-4">
<h2 class="text-center"> Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :prenom %><br />
<%= f.text_field :prenom, autofocus: true, placeholder: "Prenom", class: "form-control prenom" %>
</div>
<div class="form-group">
<%= f.label :nom %><br />
<%= f.text_field :nom, autofocus: true, placeholder: "Nom", class: "form-control nom" %>
</div>
<div class="form-group">
<%= f.label :fullname %><br />
<%= f.text_field :fullname, autofocus: false, placeholder: "Nom complet", class: "form-control fullname", :readonly => true %>
</div>
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "Password", class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
</div>
<script type="text/javascript">
$( document ).ready(function() {
$('.prenom, .nom').on('change', function(e) {
var changedFullName = $('.prenom').val() + " " + $('.nom').val();
$('.fullname').val(changedFullName);
});
});
</script>
Basically, I gave classes to your :nom, :prenom and :fullname fields and updated your jQuery, so it will work.

file upload progress bar

i need to create a progress bar for my form ,
i want when the user click on submit button a progress bar appear and start uploading the mp3
i m using ruby on rails and paperclip gem
here is my code
<% provide(:title, "Upload") %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#song, url: upload_path ) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title, t('song.title') %>
<%= f.text_field :title, class: 'form-control', required: true %>
<%= f.label :artist, t('song.artist') %>
<%= f.collection_select :artist_id, #artists, :id, :name, {prompt: "Select a Artist"}, {class: "form-control selectpicker", "data-live-search" => "true", "required" => "true" } %>
<%= f.label :mp3, t('song.mp3') %>
<%= f.file_field :mp3 %>
</br>
<%= f.submit t('song.submit'), class: "btn btn-primary", data: { disable_with: 'Uploading'} %>
<% end %>
</div>
</div>
thanks
Edit 1 :
<% provide(:title, "Upload") %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#song, url: upload_path ) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title, t('song.title') %>
<%= f.text_field :title, class: 'form-control', required: true %>
<%= f.label :artist, t('song.artist') %>
<%= f.collection_select :artist_id, #artists, :id, :name, {prompt: "Select a Artist"}, {class: "form-control selectpicker", "data-live-search" => "true", "required" => "true" } %>
<%= f.label :mp3, t('song.mp3') %>
<%= f.file_field :mp3, required: true, multiple: true, name: 'song' %>
<div class="progress-wrapper">
<p>Bitrate: <span class="bitrate"></span></p>
<div class="progress">
<div class="progress-bar" role="progressbar">
0%
</div>
</div>
</div>
</br>
<%= f.submit t('song.submit'), class: "btn btn-primary", data: { disable_with: 'Uploading'} %>
<% end %>
</div>
</div>
<script>
$(document).ready(function() {
var upload = $('#new_song');
var wrapper = upload.find('.progress-wrapper');
var progress_bar = wrapper.find('.progress-bar');
var bitrate = wrapper.find('.bitrate');
upload.fileupload({
dataType: 'script',
add: function (e, data) {
types = /(\.|\/)(mp3)$/i;
file = data.files[0];
if (types.test(file.type) || types.test(file.name)) {
data.submit();
}
else { alert(file.name + " must be mp3 file"); }
}
});
upload.on('fileuploadstart', function() {
wrapper.show();
});
upload.on('fileuploaddone', function() {
wrapper.hide();
progress_bar.width(0); // Revert progress bar's width back to 0 for future uploads
});
upload.on('fileuploadprogressall', function (e, data) {
bitrate.text((data.bitrate / 1024).toFixed(2) + 'Kb/s');
var progress = parseInt(data.loaded / data.total * 100, 10);
progress_bar.css('width', progress + '%').text(progress + '%');
});
});
</script>
this is my try but not working
it give me error :
undefined method `permit' for # Did you mean? print
and the Bitrat not showing
http://img3.stooorage.com/images/669/20501499_screenshot-14--2016-west-08-02-14.png

Why "error_message" don't show error ActiveRecord validate?

Im worked on a project that create events.But I click on a "create event" button with empty fields partial "_error_messages" don't render list with a errors.The page the page is loaded again.Validates seems to be working, in rails console I tried to create and bash show me an error "can't be blank".
This is my _event_form.html.erb
<%= form_for event, html: { class: "event-form" } do |f| %>
<%= render 'shared/error_messages', event: event %>
<% unless current_user %>
<div class='row'>
<div class='col-md-4'>
<div class="form-group col-sm-12">
<%= label :email, "Введите Ваш email:" %>
<%= text_field_tag :email, "",class: "form-control" %>
<small>Он нужен для того, что бы Вы могли позже отредактировать добавленное Вами событие.</small>
</div>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-8">
<div class="form-group col-sm-12">
<%= f.label "Название" %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group col-sm-4">
<%= f.label "Изображение" %>
<%= image_tag event.image, class: "img-responsive" if event.image.present? %>
<%= f.file_field :image %>
</div>
<div class="form-group col-sm-4">
<%= f.label "Город" %><br />
<%= select_tag :city_id, options_from_collection_for_select(City.all, :id, :name), name: "event[city_id]", placeholder: "Город" %>
<%#= f.file_field :image %>
</div>
<div class="form-group col-sm-12">
<%= f.label "Описание" %>
<%= f.text_area :description, class: "form-control", rows: 10 %>
</div>
<div class="form-group col-sm-12">
<%= f.label "Адрес" %>
<%= f.text_field :address, class: "form-control", id: :address %>
<div id='map'></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group col-sm-6">
<%= f.label "Цена от" %>
<input name="event[price][min_price]" value="<%= event.price.try(:min_price) %>" class="form-control" type="number" min="0" />
<%= f.label "Цена до" %>
<input name="event[price][max_price]" value="<%= event.price.try(:max_price) %>" class="form-control" type="number" />
<%= f.label "Тип цены" %>
<br />
<%= select_tag :price_types, options_from_collection_for_select(PriceType.all, :id, :name), name: "event[price][price_type_id]", placeholder: "Выберите тип цены" %>
</div>
<div class="form-group col-sm-6">
<%= f.label "Время проведения" %>
<%= f.text_field :time, class: "form-control", type: :time %>
</div>
<div class="form-group col-sm-6">
<%= f.label "Тип отдыха" %>
<br />
<%= select_tag :types, options_from_collection_for_select(Type.all, :id, :name, selected: event.types.pluck(:id)), name: "event[types_ids][]", placeholder: "Тип отдыха" %>
</div>
<div class="form-group col-sm-12">
<%= f.label "Дата проведения" %>
<div id="mdp"></div>
<input type='hidden' name="event[days]" class="form-control" id="mdp_alt" />
</div>
</div>
</div>
<div class="form-group text-right">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
<script>
$(document).ready(function(){
var dates = <%= event.days.pluck(:date).map{ |d| d.to_s.to_datetime.to_i * 1000 } %>;
if(dates.length){
var settings = {
altField: '#mdp_alt',
dateFormat: "yy-mm-dd",
addDates: dates
}
}else{
var settings = {
altField: '#mdp_alt',
dateFormat: "yy-mm-dd"
}
}
$('#mdp').multiDatesPicker(settings);
})
</script>
this is my _error_messages.erb
<% if event.errors.any? %>
<div id="errorExplanation">
<h2> В форме обнаружено <%= pluralize(event.errors.count, "ошибка", "ошибки") %>:</h2>
<ul>
<% event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
what's wrong?

Rails nested forms , hide remove button for first element

Hi i hava a Rails app that uses nested forms, so for example on a section you can add, as many links as you want.. also i have this other code for citizen as some user have two citizens.
My problem here is that at least you need to enter 1 citizen.. this i can make it with jquery validate, im already running that.. The problem is the Delete button that Rails insert for each field.. So i can literaly delete all fields in the form
Is there a way so just the fields added by user have the delete button.. and not all of them?
Is there any documentation about this.. i cant found it.
Here is my code in view..
<div id="nacionalidad">
<%= f.fields_for :citizens do |citizen_form| %>
<div>
<%= citizen_form.label :citizen, t('generales.citizen') %>
<%= citizen_form.select :country_id , Country.all.collect {|p| [ t("generales."+p.iso), p.id ] }, { :include_blank => true } , { :class => 'pca33' } %>
<div id="delerr"><%= citizen_form.link_to_remove t('generales.delete') %></div>
</div>
<% end %>
<%= f.link_to_add t('generales.add'), :citizens %>
</div>
The model citizen.rb
class Citizen < ActiveRecord::Base
attr_accessible :country_id
belongs_to :player
end
Thanks
Here is the full view code. Everything is working just 2 issues:
1) The delete button is present always even for first item, need to remove this delete btn for 1st item
2) If i select a country for citizen, it adds a second option, is there a way to avoid this.. and Only add the new field if the user clics on this.. add button..
Thanks!!!
Full View Code
<!-- Información personal y de contacto -->
<div id="wrapper_signup">
<%= link_to image_tag("espanol/logo.jpg", :border => 0), root_path , :class => 'logoo' %>
<div id="loggeddiv">
<%= link_to "#{current_user.email}", '#' %> |
<%= link_to (t ('generales.salir')), destroy_user_session_path, :method => :delete %>
</div>
<div id="navsteps" >
</div>
<div id="signupdiv">
<%= nested_form_for #player, :url => wizard_path do |f| %>
<div id="nombre">
<%= f.label :name %>
<%= f.text_field :name , :value => "#{current_user.name}"%>
</div>
<div id="apellidos">
<%= f.label :lastname %>
<%= f.text_field :lastname, :value => "#{current_user.lastname}" %>
</div>
<div id="cumple">
<%= f.label :birthday %>
<%= f.date_select :birthday, :start_year => 1950, :include_blank => true %>
</div>
<div id="altura">
<%= f.label :height %>
<%= f.select :height, Player::HEIGHT.each {|h| [ h, h ] } , :include_blank => true %> <%= f.select :height_measure, Player::HEIGHT_MEASURE.each {|h| [ h, h ] } %>
<%= f.select :inches, Player::INCH.each {|h| [ h, h ] }, {}, :style => (#player.inches.present? && #player.height_measure == 'pies' ? 'display: inline ' : 'display: none') %>
<%= f.label :inches, :id => 'inch_label', :style => (#player.inches.present? && #player.height_measure == 'pies' ? 'display: inline ' : 'display: none') %>
</div>
<div id="peso">
<%= f.label :weight %>
<%= f.select :weight, Player::WEIGHT.each {|w| [ w, w ] }, :include_blank => true %> <%= f.select :weight_measure, Player::WEIGHT_MEASURE.each {|h| [ h, h ] } %>
</div>
<div id="ciudad">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div id="cnatal" >
<%= f.label :birthplace %>
<%= f.text_field :birthplace %>
</div>
<div id="nacionalidad">
<%= f.fields_for :citizens do |citizen_form| %>
<div>
<%= citizen_form.label :citizen, t('generales.citizen') %>
<%= citizen_form.select :country_id , Country.all.collect {|p| [ t("generales."+p.iso), p.id ] }, { :include_blank => true } , { :class => 'pca33' } %>
<div id="delerr"><%= citizen_form.link_to_remove t('generales.delete') %></div>
</div>
<% end %>
<%= f.link_to_add t('generales.add'), :citizens %>
</div>
<div id="idiomlen">
<%= f.label :languages %>
<% for language in Language.find(:all) %>
<div class="idiomlenbox"><%= check_box_tag "player[language_ids][]", language.id, #player.languages.include?(language) %>
<%= t("languages.#{language.name}") %></div>
<% end %>
<div id="idiomlenotro">
<%= f.label :other_languages %><br>
<%= f.text_field :other_languages %>
</div>
</div>
<div id="cargafoto">
<button type="button" class="photo-button" onclick="document.getElementById('files').click();"><p> <%= t('generales.photo') %> </p></button>
<div id="upload-wrap">
<%= f.file_field :avatar , id: "files" %>
</div>
<div class="thumbperwrap">
<output id="list">
<div class="wraptocenter"><span></span>
<% if #player.avatar.present? %>
<%= image_tag #player.avatar.url(:profile), :class => "thumbper" %>
<% else %>
<img src="../assets/espanol/playersample.png" class="thumbper">
<% end %>
</div>
</output>
</div>
</div>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
document.getElementById('list').innerHTML = "";
var span = document.createElement('span');
span.innerHTML = ['<div class="wraptocenter"><span></span><img class="thumbper" src="', e.target.result,
'" title="', escape(theFile.name), '"/></div>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
<div id="videosyt">
<%= f.fields_for :links do |link| %>
<div id="videosytin">
<%= link.label :url %>
<%= link.text_field :url %>
<%= link.link_to_remove t('generales.delete') %>
</div>
<% end %>
<%= f.link_to_add t('generales.add'), :links %>
</div>
<div id="direc">
<%= f.label :cp %>
<%= f.text_field :cp %>
</div>
<div id="telef">
<%= f.label :phone %>
<%= f.text_field :phone %>
</div>
<div id="celu">
<%= f.label :cellphone %>
<%= f.text_field :cellphone %>
</div>
<div id="webpp">
<%= f.label :web_page %>
<%= f.text_field :web_page %>
</div>
<div id="next11">
<%= f.submit t('generales.next'), :class => 'siggreenc' %>
</div>
<% end %>
</div>
</div>
<%= javascript_tag do %>
$('#player_height_measure').on('change', function() {
if (this.value == 'pies') {
$('#player_inches').css('display', 'inline');
$('#inch_label').css('display', 'inline');
} else {
$('#player_inches').css('display', 'none');
$('#inch_label').css('display', 'none');
}
});
<% end %>
<style type="text/css">
#commentForm { width: 500px; }
#commentForm label { width: 250px; }
#commentForm label.error, #commentForm input.submit { margin-left: 253px; }
#signupForm { width: 670px; }
#signupForm label.error {
margin-left: 10px;
width: auto;
display: inline;
}
#newsletter_topics label.error {
display: none;
margin-left: 103px;
}
</style>
<%= javascript_tag do %>
window.onload = function() {
// validate signup form on keyup and submit
$(".edit_player").validate({ // initialize the plugin
errorElement: 'div'});
$("#player_name").rules("add", { required: true, minlength:2, messages: { required: "<%= t('generales.camporequerido') %>", minlength: "Mínimo 2 caracteres"}});
$("#player_lastname").rules("add", { required: true, minlength:2, messages: { required: "<%= t('generales.camporequerido') %>", minlength: "uh minlength?"}});
$("#player_birthday_1i").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_birthday_2i").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_birthday_3i").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_height").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_weight").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_city").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_birthplace").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_citizen").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_phone").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#player_cellphone").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
$("#files").rules("add", { required: true, messages: { required: "<%= t('generales.camporequerido') %>"}});
};
jQuery.extend(jQuery.validator.messages, {
required: "<%= t('generales.camporequerido') %>",
remote: "Please fix this field.",
email: "Ingresa un correo electrónico válido.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
minlength: jQuery.validator.format("Please enter at least {0} characters."),
rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
range: jQuery.validator.format("Please enter a value between {0} and {1}."),
max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
<% end %>
You can use jquery first selector to remove the first delete button based on the id or classname of the parent div (which you aren't currently setting)
<div class="citizens_update">
<%= citizen_form.label :citizen, t('generales.citizen') %>
<%= citizen_form.select :country_id , Country.all.collect {|p| [ t("generales."+p.iso), p.id ] }, { :include_blank => true } , { :class => 'pca33' } %>
<div id="delerr"><%= citizen_form.link_to_remove t('generales.delete') %></div>
</div>
And then use jquery to find the first instance of delerr in the citizens_update div and remove it
$(".citizens_update").find("#delerr:first").remove
http://jsfiddle.net/tiri/ayTrw/

show rest of a form if a checkbox is ckecked in ruby on rails

I need to ask to my user if will pay a service with credit card...if it checked the option pay_with_card? it must show the rest of the form, that ask for other data like card number, mail, etc. if the user don't checked it, it must show a message, the question is...how can I do this? thanks in advance
<%= form_for(#product) do |f| %>
<%= f.label :pay_with_card? %>
<%= f.check_box :pay_with_card,{}, "Yes", "No"%>
<div>
<%= f.label :card_number %> <%= f.text_field :card_number %>
</div>
<div>
<%= f.label :mail %> <%= f.text_field :mail %>
</div>
<% end %>
Make the card number/mail details div style="display:none;", then add some javascript to the checkbox to change it to display:block;
Something like this:
<%= form_for(#product) do |f| %>
<%= f.label :pay_with_card? %>
<%= f.check_box :pay_with_card,{}, "Yes", "No"%>
<div id="card_details" style="display:none;">
<%= f.label :card_number %> <%= f.text_field :card_number %>
<%= f.label :mail %> <%= f.text_field :mail %>
</div>
<% end %>
<script type="text/javascript">
var checkbox = document.getElementById('product_pay_with_card');
var details_div = document.getElementById('card_details');
checkbox.onchange = function() {
if(this.checked) {
details_div.style['display'] = 'block';
} else {
details_div.style['display'] = 'none';
}
};
</script>
How about using jQuery?
First, wrap your credit card fields in a div with class credit_card_fields and than add this JS code to your page:
$("input[type='checkbox']#pay_with_card").on('change', function(){
$('.credit_card_fields').toggle();
});
You can use JS for it or move pay_with_card out of form like:
<%= link_to 'pay with card', your_current_path(:pay_with_card => 1) %>
<%= form_for(...) do |f| %>
<% if params[:pay_with_card] %>
<%= # fields for card %>
<% end %>
<% end %>
You can do it through jQuery, for example:
$ ->
$('select#pay_with_card').change ->
if $(this).val() == 'yes'
$('.card_block').slideDown('fast')
else
$('.card_block').slideUp('fast')
assumed that part of the form with payment card is included in the div with .card_block class
Ok my solution is this: all the code in the view, if a user check pay_with_card...(mi code is in spanish) it shows the complete form...if is not checked don´t show nothing, just the same checkbox asking for payment... thanks guys.
function mostrar (){
var checkbox = document.getElementById('chk_tarjeta');
if (checkbox.checked)
document.getElementById("card_details").style.display = "block";
else
document.getElementById("card_details").style.display = "none";
</script>
<h1>Forma de Pago</h1>
<%= form_for(#product) do |f| %>
<div id="product_pay_with_card">
<div >
<%= f.label :paga_con_tarjeta? %></br>
<%= f.check_box :paga_con_tarjeta, :id => "chk_tarjeta", :onclick => "mostrar();" %>
<div>
</div>
</div>
<div id="card_details" >
<div>
<%= f.label :numero_de_tarjeta %></br>
<%= f.text_field :numerotarjeta %>
</div>
<div>
<%= f.label :codigo_de_seguridad %></br>
<%= f.text_field :codigoseguridad %>
</div>
This worked for me with a form_with model and bootstrap
Change my_hidden_form with an id that makes sense for your form.
Original code is haml
= form_with scope: :model, url: models_path, local: true do |form|
.row
.col-6
.form-group
%h5.mb0 THE CASE TO TICK
= form.check_box :form_value, {:data => {:aria => {controls: :my_hidden_form, expanded: false}, :toggle => "collapse", :type => "checkbox", :target => "#my_hidden_form" }}
.row
.col-6
.form-group.collapse#my_hidden_form
%h5.mb0 THE FORM TO SHOW WHEN CASE IS TICKED
= form.text_field :name, placeholder: "A name"
.row
.col-md-12.text-right
= form.submit 'Submit', class: "btn btn-primary"
Converted to erb/html with https://haml2erb.org/
<%= form_with scope: :model, url: models_path, local: true do |form| %>
<div class="row">
<div class="col-6">
<div class="form-group">
<h5 class="mb0">THE CASE TO TICK
<%= form.check_box :form_value, {:data => {:aria => {controls: :my_hidden_form, expanded: false}, :toggle => "collapse", :type => "checkbox", :target => "#my_hidden_form" }} %>
</h5>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group collapse" id="my_hidden_form">
<h5 class="mb0">THE FORM TO SHOW WHEN CASE IS TICKED
<%= form.text_field :name, placeholder: "A name" %>
</h5>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<%= form.submit 'Submit', class: "btn btn-primary" %>
</div>
</div>
<% end %>
Since the approach suggested by #Unixmonkey didn't work for me, here's a slight variation I put together using an event listener.
<script type="text/javascript">
const checkbox = document.getElementById('product_pay_with_card');
const details_div = document.getElementById('card_details');
checkbox.addEventListener("change", (event) => {
if (event.currentTarget.checked) {
details_div.style['display'] = 'block';
}
else {
details_div.style['display'] = 'none';
}
});
</script>

Resources