I have some form fields for email, name, phone, ID etc, that I want to populate when a form loads with current_user.email etc. The values for the form fields are stored in a db field called default_value. The following works, but it puts the literal string current_user.email in the form field. How do I get it to actually output the email and other values so it looks like myname#stack.com?
<%= form.text_field field.name, required: field.required, value: field.default_value, class: "form_control" %>
Related
I need to populate data in text filed based on given input.Below the code i am working on.
emp_form.html.erb
'<%= f.label "Name:"%> <%= f.text_field :emp_name %>
<%= f.label "Emp Id:" %> <%= f.text_field :emp_id %>
<%= f.label "Roll:" %> <%= f.text_field :emp_roll %>'
If i am entering employee name in text field,it has to fetch emp_id and emp_roll value from sqlite data base and populate its value in respective text fields in the same form. How to implement this?
Thanks,
Raja
Hello #Raja I have created a sample application for your problem below is the link for code on GitHub
Auto populate form values for search
I have written a simple ajax call for fetching the employee details based on the user typing the name on name field if values found that ajax will assign the values to the specified field.
First, you need to create an employee after taking the clone of code.
I have a problem with my code below. Essentially, I have a boolean that determines if a client already exists or not - if user says 'yes' shows a previous list of clients using collection_select - if user says 'no' they get an input field to create a new client using text_field. JQuery shows or hides the correct input field accordingly.
Problem: When submitting this form, even though JQuery is hiding the field that's not relevant, that field is affecting or preventing the form from being submitted.
For example: If user says 'yes' and chooses an existing client and form is submitted, I get an error message the client_name is blank (because the form is submitting the blank text_field instead of what user selected in collection_select)
Any ideas how I can fix this? Appreciate any help.
<p> Is this new collection for an existing client? </p>
<%= select_tag(:existing_client,options_for_select([['Yes', 1], ['No', 2]], 2), {id: "existing-client"}) %>
<%= f.collection_select :client_name, current_designer.client_folders, :client_name, :client_name, {prompt: true, selected: :id}, {id: "existing-list"} %>
<%= f.text_field :client_name, placeholder: "e.g. Sally Smith", id: "collect-input" %>
This Post helped me solve this. I needed to disable the hidden field and the form submits.
I need to manipulate the user's entry before submitting it for search. I'm using the ransack gem for Rails. I want to search through three columns: street address, city, and zip code. Street address and city are working fine. But since zip is an integer, I'm having a conflict between the string entry and the integers in the database. Without creating a new field, is there some way I can use "to_f" or something similar to convert the user's entry before submitting it? Here's my search form.
<%= search_form_for #search do |f| %>
<%= f.text_field :address_or_city_cont, :placeholder => 'Enter Street Address OR City' %>
<%= f.submit "Search Homes", class: "btn btn-large btn-primary" %>
<% end %>
The form input will always come in as a string, but what you can do is manipulate the data on the server side to convert the input to an integer using .to_i where building up the search query.
I have a form that allows the user to search for existing records to populate an association. Each "Booking" belongs to a "Customer". So the form allows you type the customer's name, it does a search automatically, and you click the customer you want. The input field you're typing into should display the customer's name, but for the form to work, I set the customer_id in a hidden input field.
I'm using the simple_form gem. Does anybody know if I can display the validation errors for the customer_id next to the text input field that displays the customer's name? The customer_id is required in the model, so I need the form to tell the user that if they leave it blank.
View code (simplified -- there's some JavaScript that handles searching when you type into the customer text box, and that sets the value in the hidden field to the customer's id when you make a selection):
<%= simple_form_for #booking do |f| %>
<%= f.hidden_field :customer_id, id: "customer_id" %>
<%= f.input :customer, required: true,
input_html: { value: #booking.customer_name } %>
<% end %>
I eventually found out about the append block in simple_form (buried in a pull request, not documented anywhere else that I could find). Basically, you can use that to append whatever markup you want to your f.input. I did the following, which is pretty hacky, but it does what I need it to do:
<%= f.input :customer, required: true,
wrapper_html: { class: ("error" if #booking.errors[:customer_id].any?) } do %>
<%= f.input_field :customer, value: #booking.customer_name %>
<%= f.error :customer_id %>
<% end %>
First, I had to conditionally add the class "error" to the wrapper if there were any errors on the related field, then I used the append block to render out the input field, followed by the errors for the related field from the model. Hope this helps someone in the future!
How do I create a column in my table that is an input field?
i cant find it anywhere,
I'm looking to have the column for a price that is set at nil/empty, whatever.
Then the user inputs a price which is validated against a reserver price column, if successful it is added to cart.
It's the first part I'm having bad trouble with as my cart is set up already
You'll need a migration that adds a string field do your database. Check out the Rails guide for information on migrations, but it's essentially this:
add_column :my_table, :my_field, :string
Then it will automatically be cast to a string when retrieved from the database. Once you have that field, you can use the form helpers:
<%= form_for #my_model do |f| %>
<%= f.text_field :my_field %>
<%= f.submit %>
<% end %>
You can use text_field like this : http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag.