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.
Related
I have created a form,
<%= semantic_form_for [:admin, #resource], builder: ActiveAdmin::FormBuilder do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :resource_template, :as => :select, :collection => Resource.select {|r| r.resource_template ==true }, :input_html => { :style => 'width: 20%;'} %> //added now ,just to try out how it may work.
<%= link_to 'COPY', admin_root_path, class: 'myButton' %> //added now,just to try out how it may work.
<%= f.input :title %>
<%= f.input :url %>
<%= f.actions %>
This form helps us creating new resources, now I have added a drop-down to select old existing resources.
How can i pre-fill the rest of form after clicking COPY of whatever value selected in the dropdown list?
User might edit some prefill value and create its own new resource accordingly.
Unless you're OK with submitting the form immediately after the user selects an option, which id probably a bad idea, you're going to have to do this with JavaScript.
The exact implementation depends largely on how your app is configured, but the general approach would probably be something like:
Add an endpoint in your resources controller that accepts an POST request with an :id param and returns a JSON version of your resource
Add a change listener on your select box that sends a POST request to the endpoint in step 1. You'll also have to send the resource id as a param.
If the response from the endpoint in step 1 returns any data, use the keys and values in the JSON data to fill the form fields accordingly
This is a fairly broad question, so it's hard to give you a more detailed answer without knowing which JavaScript frameworks you have in your app, but this should point you in the right direction.
On clicking of copy button you need to fire an ajax request with option selected in the select box, get data in controller with which you want to fill the form, in success callback of your ajax request , fill the fields returned from the ajax request.
I'm trying to submit a RoR Form. Strong Params are fine as I'm not getting a not permitted message in the logs. For some reason, I keep getting a this field cannot be blank error message for the below field. The logs say the client_name field is blank even though I've selected something. There's a has-many/belongs-to relationship between designer & client-folder.
When I choose one of the existing client_folder from the dropdown and press submit, it gives the error message. If I use a text field and simply type a name, it works fine, so I believe it's just something with collection_select. Where am I going wrong?
<%= form_for([#designer, #client_folder]) do |f| %>
<%= f.collection_select :client_name, current_designer.client_folders, :id, :client_name, {}, {id: "existing-list", required: true} %>
<%= f.submit "Create" %>
<% end %>
I have a Rails 4, that uses Rails' default form (I am NOT using Simple Form).
—————
EDIT: although the goal is fairly similar (allowing users to submit a form by pressing the enter key instead of pushing a submit button), my question is different from Form submitting when pressing enter in Textarea since it relates to Rails forms, which have a particular behavior and do not work in the same way as regular jQuery forms.
—————
One of my forms allows users to create comments:
<%= form_for([post, post.comments.build]) do |f| %>
<p>
<%= f.text_area :body, :placeholder => "New comment", size: "15x1" %><%= f.submit id: 'create_comment'%>
</p>
<% end %>
I would like to get rid of the <%= f.submit id: 'create_comment'%> part and allow users to submit a new comment just by pressing the enter key.
How can this be achieved?
If you use a f.text_field rather than text_area it should submit the form on enter by default.
If you need to use a textarea, then it will have to be javascript. something like:
$('#id_of_textarea').keypress(function(e){
if(e.which == 13){
$(this).closest('form').submit();
}
});
I am currently trying to keep a checkbox in form which enables the text field up it is checked So how can I do it. I am using simple form.
this is simple form
<%= f.input :satisfaction %>
I have tried keeping as below
<%= f.input :satisfaction, as: :boolean %>
But is only displaying checkbox not a text field.
So can any one tell me how to this.
Your syntax is invalid. You've missed a comma between the two arguments. Try:
<%= f.input :satisfaction, as: :boolean %>
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!