concatenate fields in another fields with auto refresh - ruby-on-rails

I m new in rails and html....
I want to concatenate first name and last name in a field fullname in a view. I want that when you modify last name or first name, full name is automatically refreshed with 'first name' +" " + 'lastname'
I tried this but Ive an error. because, seems that full name field stays empty and I tried to make an alert in function to see if concatenation is working. But seems not to do it....
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<script type="text/javascript">
function test ()
{
$('fullname').value <= $('firstname').value + " " + $('lastname').value;
}
</script>
<div class="form-group">
<%= f.label :prenom %><br />
<%= f.text_field :firstname, autofocus: true, placeholder: "Prenom", class: "form-control",:onchange => "test()" %>
</div>
<div class="form-group">
<%= f.label :lastname %><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: true, placeholder: "Full Name", class: "form-control", :readonly => true %> <!-- ajout de readonly -->
</div>
Thanks for helping

You should check (or post to your question) your resulting html so I'll may help you more.
This is incorrect:
$('fullname').value <= $('firstname').value + " " + $('lastname').value;
Here the working example.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<script type="text/javascript">
$( document ).ready(function() {
$('input.firstname, input.lastname').on('input', function(e) {
var changedFullName = $('input.firstname').val() + " " + $('input.lastname').val()
$('input.fullname').val(changedFullName);
});
});
</script>
<label for="firstname">FirstName</label><br>
<input type="text" name="firstname" class="firstname" /><br>
<label for="lastname">LastName</label><br>
<input type="text" name="lastname" class="lastname" /><br>
<hr>
<span>FullName</span><br>
<input type="text" name="fullname" class="fullname" readonly />
</form>

Related

Set maxlength in html input without changing form width?

I have a sign up form and want to limit the length of the First Name and Last Name fields to 50 characters.
The problem is, when I do, the form displays much wider.
How can I make sure the user cannot enter more than 50 characters into those form fields without it affecting the width of the form?
Reference
Here is the code/result before and after (the only change is the addition of the maxlength: 50 to the form helper.
Before:
<div class="row">
<div class="col-6">
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, autocomplete: "First Name", class: "form-control" %>
</div>
</div>
<div class="col-6">
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autocomplete: "Last Name", class: "form-control" %>
</div>
</div>
</div>
<br>
After (too wide):
<div class="row">
<div class="col-6">
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, maxlength: 50, autofocus: true, autocomplete: "First Name", class: "form-control" %>
</div>
</div>
<div class="col-6">
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, maxlength: 50, autocomplete: "Last Name", class: "form-control" %>
</div>
</div>
</div>
<br>
I hadn't realised, but when rails sees maxwidth: 50, it automatically adds: size: 50.
I simply add a size: 25 to the form field helper and the width of those fields becomes 25 rather than 50
<div class="row">
<div class="col-6">
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, maxlength: 50, size: 25, autofocus: true, autocomplete: "First Name", class: "form-control" %>
</div>
</div>
<div class="col-6">
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, maxlength: 50, size: 25, autocomplete: "Last Name", class: "form-control" %>
</div>
</div>
</div>
<br>

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.

Ruby on Rails Devise edit registration form with ability to select or add a separate model (e.g. Company)

I have a User model and a Company model (amongst others) and an "Edit Profile" page with a form. One field of the form is to select a company from a list and save to the Users table in a column called "company_id", which is linked to the ID of each Company in the Companies table.
This all works great.
However, I would prefer to have a text field in which a user can start typing a company name and select a suggestion or create a new Company if theirs is not in the database.
I'm new to ruby and would like to know the best way to do this if anyone can help.
app\views\devise\registrations\edit.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<div id="errorexplanationcontainer"><%= devise_error_messages! %></div>
<div class="mdl-textfield mdl-js-textfield">
<%= f.text_field :first_name, class: 'mdl-textfield__input', autofocus: true %>
<label class="mdl-textfield__label" for="first_name">First name</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<%= f.text_field :last_name, class: 'mdl-textfield__input' %>
<label class="mdl-textfield__label" for="last_name">Last name</label>
</div>
<!-- Company Field -->
<div class="field">
<%= f.label :company_id %>
<%= f.select :company_id, Company.all.collect {|company| [company.name, company.id]}, {:include_blank => "Select One"}, id: :job_company_id %>
</div>
<!-- End -->
<div class="mdl-textfield mdl-js-textfield">
<%= f.text_field :job_title, class: 'mdl-textfield__input' %>
<label class="mdl-textfield__label" for="job_title">Job Title</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<%= f.email_field :email, class: 'mdl-textfield__input' %>
<label class="mdl-textfield__label" for="email">Email</label>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="mdl-textfield mdl-js-textfield">
<%= f.password_field :password, class: 'mdl-textfield__input', autocomplete: "off" %>
<label class="mdl-textfield__label" for="password">New password</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<%= f.password_field :password_confirmation, class: 'mdl-textfield__input', autocomplete: "off" %>
<label class="mdl-textfield__label" for="password_confirmation">Confirm password</label>
</div>
<button id="show-dialog" type="button" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
UPDATE
</button>
<dialog class="mdl-dialog">
<h4 class="mdl-dialog__title">Save Changes</h4>
Re-enter your password to save changes to your account.
<div class="mdl-textfield mdl-js-textfield">
<%= f.password_field :current_password, class: 'mdl-textfield__input', autocomplete: "off" %>
<label class="mdl-textfield__label" for="current_password">Current password</label>
</div>
<div class="mdl-dialog__actions">
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
<%= f.submit "UPDATE", class: 'hideinput' %>
</button>
<button type="button" class="mdl-button close">CANCEL</button>
</div>
</dialog>
<script>
var dialog = document.querySelector('dialog');
var showDialogButton = document.querySelector('#show-dialog');
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
showDialogButton.addEventListener('click', function() {
dialog.showModal();
});
dialog.querySelector('.close').addEventListener('click', function() {
dialog.close();
});
</script>
<% end %>
I have used Twitter typeahead in on of my projects. It is pretty smooth and light, you just have to pass an array to it and it does the trick for you.
Some detailed documentation and examples are listed here: https://twitter.github.io/typeahead.js/examples/
Bundle install twitter-typeahead-rails
In your application.js, add the following line above require_tree:
//= require twitter/typeahead.min
In your controller, you can initialize the array:
#companies = Company.pluck(:name)
In your view:
<div id='search'>
<%= f.text_field :company, class: 'mdl-textfield__input' %>
</div>
Javascript:
var companies = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: #{#companies},
});
$('#search .mdl-textfield__input').typeahead('destroy').typeahead({
hint: true,
highlight: true,
minLength: 2
},
{
source: companies
});

Reder class for a collection_radio_buttons within simple_form

I am using simple_form to create a form within my Rails project.
<%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post, id: 'invitations-form', class: 'invitations-form'} do |f| %>
<%= f.input :first_name, label: false, placeholder: "Family Member's First Name", input_html: { maxlength: 15, size: 40, value: nil } %>
<%= f.input :last_name, label: false, placeholder: "Family Member's Last", input_html: { maxlength: 15, size: 40, value: nil } %>
<%= f.input :email, label: false, placeholder: "Enter Email Address", input_html: { value: nil } %>
<div class="radio_buttons">
<%= f.label "#{:gender}:"%>
<%= f.collection_radio_buttons :gender, [["male", "male"] ,["female", "female"]], :first, :last %>
</div>
<%= f.input :invitation_relation, label: false, prompt: "I am this person's:", collection: relation_types, label_method: :humanize, input_html: { class: "browser-default dropmodule__input-select" } %>
<%= submit_tag "Send Invitation" %>
<% end %>
I have used the collection_radio_buttons method to create, well, a collection of radio buttons. I'm not sure this is relevant, but the reason I used this method instead of f.input :foo, as: :radio_buttons was due to the fact that the input method wasn't producing a label tag for me, which I think I could now produce using label_html. I'm not sure if this would be a viable alternative, but this bug is bothersome to me due to the time I've spent on it.
Here's the html generated output for the relevant div above:
<div class="radio_buttons">
<label class="string optional control-label" for="user_gender:">Gender:</label>
<span>
<input checked="checked" id="user_gender_male" name="user[gender]" type="radio" value="male">
<label class="collection_radio_buttons" for="user_gender_male">male</label>
</span>
<span>
<input id="user_gender_female" name="user[gender]" type="radio" value="female">
<label class="collection_radio_buttons" for="user_gender_female">female</label>
</span>
</div>
I need the input tag for each radio_button to contain a class of "with-gap", like so:
<input checked="checked" id="user_gender_male" class="with-gap" name="user[gender]" type="radio" value="male">
and
<input id="user_gender_female" class="with-gap" name="user[gender]" type="radio" value="female">
but I have been unsuccessful. Although I really want to solve the issue by placing the class within the label of the collection_radio_buttons method, if there is an alternative within simple_form please show this as a solution.
OK, I found the answer here:
https://stackoverflow.com/a/30619068/4379077
What I needed to do was add an item_wrapper_class as the 5th argument to the method. It doesn't look like it's optional. When I try to remove it from the argument list my class argument (the 6th arg in the method) gets ignored. Here's the final input:
<div class="radio_buttons">
<%= f.label "#{:gender}:"%>
<%= f.collection_radio_buttons :gender, [["male", "male"] ,["female", "female"]], :first, :last, {:item_wrapper_class => 'foo-bar'}, {:class => "with-gap" } %>
</div>
and final output:
<div class="radio_buttons">
<label class="string optional control-label" for="user_gender:">Gender:</label>
<span class="foo-bar">
<input checked="checked" class="with-gap" id="user_gender_male" name="user[gender]" type="radio" value="male">
<label class="collection_radio_buttons" for="user_gender_male">male</label >
</span>
<span class="foo-bar">
<input class="with-gap" id="user_gender_female" name="user[gender]" type="radio" value="female">
<label class="collection_radio_buttons" for="user_gender_female">female</label>
</span>
</div>
If anyone knows how to make the inline_wrapper_class optional please let me know.

Rails form Images

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.");
}
});
});

Resources