ruby refresh concatenate field - ruby-on-rails

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.

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>

I want to centralize a devise simple-form in a rails project with div class="col-md-6 col-md-offset-3"

I have tried to put a div block, with class="col-md-6 col-md-offset-3", around my devise simple_form expecting that this would take 1/3 of my screen width and would be centralized (offset). It did not worked. Why ?
<div class="col-md-6 col-md-offset-3">
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email,
required: true,
autofocus: true ,
input_html: { autocomplete: "email" }%>
<%= f.input :password,
required: true,
hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
</div>
The class is offset-md-3 not col-md-offset-3
<div class="row">
<div class="col-md-6 offset-md-3">
...
</div>
</div>

How can I use optional attributes on an object using Ruby on Rails?

I have several attributes on a Player model. My form contains fields for a player to be added. I want to have optional fields where a second player can enter their name, and rather than having to retype the address and other things for the second player, I want the second player to inherit all the other information that the first player entered without having to type it in again. How can I do this?
players/_form.html.erb
<%= form_for #player, html: {class: "horizontal-form"} do |player| %>
<div class="form-group">
<div class = row>
<%= player.label :first_name, "First Name", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :first_name, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :last_name, "Last Name", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :last_name, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :address, "Address", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :address, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :city, "City", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :city, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :state, "State", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :state, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :zip_code, "Zip Code", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :zip_code, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :email, "Email", class: "col-sm-2 control-label"%>
<div class="col-sm-offset-2">
<%= player.text_field :email, class: "form-control", :required => true %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :veteran, "Veteran", class: "col-sm-2 control-label"%>
<div class="checkbox col-sm-offset-3">
<%= player.check_box :veteran %>
</div>
</div>
</div>
<div class="form-group">
<div class = row>
<%= player.label :branch_id, "Branch", class: "col-sm-2 control-label" %>
<%= player.collection_select(:branch_id, Branch.all, :id, :name, :prompt => true) %>
</div>
</div>
<%= player.submit 'Submit', class: 'col-sm-2 col-sm-offset-2 btn btn-primary' %>
<% end %>
Thank you for any suggestions!
This is probably more of a javascript question than a RoR one.
Assuming you want them to have the option to fill in different information, the easiest way I can think to do this is to have a bunch of hidden fields for the player two information (other than their name), give the player one fields onkeypress events that would call a function that would take in the id of the corresponding player two field and use that to find the hidden player two field and fill in their html to be the same as the field you're currently filling in, something like
function fillPlayerTwoField(id) {
$(id).text(this.value)
}
then if they start filling in the player two name unhide all the player two fields so the have the option to change that information if they so choose

Rails4: How to apply Bootstrap to simple_nested_form_for

I'd like to apply bootstrap to the input fields as following.
<div class="input-group">
<span class="input-group-addon">Title</span>
<input type="text" class="form-control">
</div>
My current code is as following.
_form.html.erb
<%= simple_nested_form_for #event do |f| %>
<%= f.input :title %>
<%= f.input :description, input_html: { rows: 5, cols: 30 } %>
<%= f.input :charge_person %>
.
.
.
<% end %>
It would be appreciated if you could give me the best way in Rails.
This should do the trick:
<%= simple_nested_form_for #event do |f| %>
<div class="input-group">
<span class="input-group-addon">Title</span>
<%= f.input :title, class: 'form-control' %>
</div>
<div class="input-group">
<span class="input-group-addon">Description</span>
<%= f.input :description, class: 'form-control' %>
</div>
<div class="input-group">
<span class="input-group-addon">Charge Person</span>
<%= f.input :charge_person, class: 'form-control' %>
</div>
...
<% end %>
You shouldl read more detail SIMPLE FORM DOCS
<%= simple_nested_form_for #event, , wrapper_html: { class: 'input-group' } do |f| %>
<%= f.label :title, wrapper_html: { class: 'input-group-addon' } %>
<%= f.input_field :title, wrapper_html: { class: 'form-control' } %>
.
.
.
<% end %>

Devise: UNIQUE constraint failed: users.username

When I create a new user with an existing username I get this error:
UNIQUE constraint failed: users.username
I do get an error message for everything else (already used email, uncorresponding passwords).
How can I make it return an error that shows up on the page, just like the others, instead of a crashing error.
I have all the default devise setting haven't changed a thing.
Thank you for the help.
EDITED:
This is the page where I create my user:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {class: "form-horizontal form-user form1"}) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :username, class: "control-label mr" %><br>
<%= f.text_field :username, class: "text-field form-control", html: {spellcheck: "false"} %>
</div>
<div class="form-group">
<%= f.label :email, class: "control-label mr" %>
<%= f.email_field :email, class: "text-field form-control", html: {spellcheck: "false"} %>
</div>
<div class="form-group">
<%= f.label :password, class: "control-label ml" %>
<%= f.password_field :password, class: "text-field form-control", html: {autocomplete: "off"} %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirm Password", class: "control-label ml" %>
<%= f.password_field :password_confirmation, class: "text-field form-control", html: {autocomplete: "off"} %>
</div>
<div class="form-group">
<div class="center">
<br><%= f.submit "Next", class: " btn btn-primary" %>
</div>
</div>
<% end %>

Resources