Set maxlength in html input without changing form width? - ruby-on-rails

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>

Related

rails 6 save data to multiple tables from one controller

I'm still really new to rails. I have a site that allows tournament directors to host online registration. When they setup the registraiton form they can add custom fields for different section like basic info, division, etc. That part is working but when the user goes to the registraiton form, I am able to pull the fields into the form but I don't know how to save them to a separate table. I have a participants table and the custom fields are in a fields table.
When a user registers or edits their registraion info, how do I save the input for the custom fields into a separate table. I have a table called field_answers with participant_id, field_id and answer, but can't figure out how to save the answers from the participants_controler.
Below is the form for the basic section of the registraiton form. The #basic.each section is where the custom fields are being pulled in, and I'm guessing I'll need to give the fields unique names rather than just answer then save them but I don't know how to do that.
<%= form_with model: [#event, #participant], class: "shadow p-3 mb-3 rounded text-dark", local: true do |f| %>
<%= f.hidden_field :child_id, value: #cid %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :event_id, value: #tID %>
<%= f.hidden_field :basic_complete, value: 1 %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :first_name %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :first_name, class: 'form-control', value: #part.first_name, placeholder: "First name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :last_name %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :last_name, class: 'form-control', value: #part.last_name, placeholder: "Last name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :date_of_birth %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.date_field :dob, class: 'form-control', value: #part.dob, placeholder: "Date of Birth" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :gender %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<label class="main">Female
<%= f.radio_button :gender, "female", checked: true if #part.gender == "female" %>
<span class="w3docs"></span>
</label>
<label class="main">Male
<%= f.radio_button :gender, "male", checked: true if #part.gender == "male" %>
<span class="w3docs"></span>
</label>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :address1 %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :address1, class: 'form-control', value: #part.address1, placeholder: "Address 1" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :address2 %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :address2, class: 'form-control', value: #part.address2, placeholder: "Address 2" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :city %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :city, class: 'form-control', value: #part.city, placeholder: "City" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :state %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :state, class: 'form-control', value: #part.state, placeholder: "State" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :country %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :country, class: 'form-control', value: #part.country, placeholder: "Country" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :zip %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :zip, class: 'form-control', pattern: "[0-9]*", value: #part.zip, placeholder: "Postal Code" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :phone %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :phone, class: 'form-control', pattern: "[0-9]{3}-[0-9]{3}-[0-9]{4}", maxlength: "12", value: #part.phone, placeholder: "Phone" %>
</div>
</div>
<% #basic.each do |b| %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label b.name %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :answer, class: 'form-control' %>
<%= f.hidden_field :field_id, value: b.id %>
<%= f.hidden_field :event_id, value: #tID %>
</div>
</div>
<% end %>
<div class="form-group row">
<div class="col-md-8 col-sm-12"></div>
<div class="col-md-4 col-sm-12">
<%= f.submit "Register", class: "profile_btn" %>
</div>
</div>
<% end %>
Here is my participants_controller.rb:
class ParticipantsController < ApplicationController
before_action :event
before_action :participant, only: %i[create]
def new
#participant = #event.participants.new
if current_user
#user = User.find(current_user.id)
#children = #user.children
end
#basic = Field.where(event_id: #event.id, field_type: 'basic')
end
def create
byebug
if #participant.save
flash[:success] = 'Child was successfully created.'
#redirect_to edit_event_participant_path(#participant.id)
redirect_to "/events/#{#event.id}/participants/#{#participant.id}/edit?rt=#{#participant.child_id != nil ? 'child' : 'self'}&step=2"
else
byebug
flash[:notice] = #participant.errors.full_messages.join('<br />').html_safe
redirect_back(fallback_location: events_path)
end
end
def show
end
def edit
#participant = Participant.find(params[:id])
#child = Child.find(#participant.child_id)
#custom_basic = Field.find_by(event_id: #participant.event_id, field_type: 'basic')
end
def update
end
private
def event
#event = Event.find_by(id: params[:event_tourn_url])
end
def participant
#participant = event.participants.new(participant_params)
end
def participant_params
params.require(:participant).permit(:event_id, :user_id, :child_id, :first_name, :last_name, :dob, :gender, :address1, :address2, :city, :state, :country, :zip, :phone, :basic_complete)
end
end
I'd appreciate any help.
Edit:
I edited my participant_params in the controller to:
def participant_params
params.require(:participant).permit(:event_id, :user_id, :child_id, :first_name, :last_name, :dob, :gender,
:address1, :address2, :city, :state, :country, :zip, :phone,
participant_fields_attributes: [:id, :participant_id, :field_id, :answer])
end
And my participants model is:
class Participant < ApplicationRecord
belongs_to :event
has_many :participant_fields
accepts_nested_attributes_for :participant_fields
When I submit all the correct fields are added to the participants table but nothing to the participant_fields table. And in the logs I get Unpermitted
parameters: :answer, :field_id

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.

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

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 %>

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?

Resources