In a checkout I'm prompting a user to confirm his address and required_date to receive the part. The form is shown for the #auction attributes that are needed to be confirmed. All of the address lines are already populated. But the date field is cleared, I want the date field to be populated with the date that was already chosen(#auction.required_date), so that the user doesn't have to re-pick the date if the required_date has not changed.
Here's the form
.form-container
%p#alert= alert
= form_for(#auction, html: { class: "inline-form" }, remote: true ) do |f|
- if #auction.errors.any?
#error_explanation
%h2
= pluralize(#auction.errors.count, "error")
prohibited this auction from being saved:
%ul
- #auction.errors.full_messages.each do |message|
%li= message
.field.form-group
= f.text_field :destination_address, class: "form-control", placeholder: "Destination Street Address"
.field.form-group
= f.text_field :destination_city, class: "form-control", placeholder: "Destination City"
.field.form-group
= f.text_field :destination_state, class: "form-control", placeholder: "Destination State/Region"
.field.form-group
= f.text_field :destination_zip, class: "form-control", placeholder: "Destination Zip Code"
.field.form-group
= f.text_field :destination_country, class: "form-control", placeholder: "Destination Country"
.field
= f.date_field :required_date, class: "form-control datepicker", placeholder: "Required Delivery Date"
%br/
.actions.submit_button
= f.submit 'Submit', class: "btn custom-btn"
%br/
:javascript
$('.datepicker').pickadate({
selectMonths: true
});
I believe you can just do this:
= f.date_field :required_date, class: "form-control datepicker", placeholder: "Required Delivery Date", value: #auction.required_date
and it should populate the date field.
I suppose you have to add value in right format using strftime, for example:
value: #auction.required_date.strftime('%m/%d/%Y')
Related
rails 6.0.3
bootstrap 5
I'm sorry if there is a simple answer, or if it's not even possible to change the following:
I currently have a select dropdown on my form,
<div class="field mb-3">
<%= form.label :Please_select_document_type %>
<%= form.select(:priority, [['Critical'],['Moderate'], ['Low']], { :include_blank => '-- Select One --' }, {class: "form-check"}) %>
</div>
Drop down menu
I have written below a radio button with the three same priority levels. How do I get the radio button to store the correct value to priority when it is selected?
<%= form.label :priority %><br>
<div class="btn-group" data-toggle="buttons-radio">
<%= form.radio_button :priority, 'Low', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "success-outlined", autocomplete: "off", checked: true %>
<%= form.label :priority, class: "btn btn-outline-success", for: "success-outlined", value: "Low" %>
<%= form.radio_button :priority, 'Moderate', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "warning-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-warning", for: "warning-outlined", value: "Moderate" %>
<%= form.radio_button :priority, 'Critical', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "danger-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-danger", for: "danger-outlined", value: "Critical" %>
</div>
Radio button
The radio buttons should have the name <obj>['priority']. The code in the question does not work since it uses a different name for each radio button. Remove the name key from the radio_button method call and let Rails add it for you.
<%= form.radio_button :priority, 'Low', type: "radio", class: "form-check btn-check", id: "success-outlined", autocomplete: "off", checked: true %>
<%= form.label :priority, class: "btn btn-outline-success", for: "success-outlined", value: "Low" %>
<%= form.radio_button :priority, 'Moderate', type: "radio", class: "form-check btn-check", id: "warning-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-warning", for: "warning-outlined", value: "Moderate" %>
<%= form.radio_button :priority, 'Critical', type: "radio", class: "form-check btn-check", id: "danger-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-danger", for: "danger-outlined", value: "Critical" %>
I want to have form for possible answers in poll and two submit buttons. First one passing all text fields, second one passing one of these fields as nil causing validation error. This is my _form.html.haml file:
= form_for [#poll, #question] do |f|
- if #question.errors.any?
#error_explanation
%h2= "#{pluralize(#question.errors.count, "error")} prohibited this question from being saved:"
%ul
- #question.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :title
= f.text_field :title
%p
= f.label :kind
.radio
-#kind_options.each do |option|
%label
=f.radio_button :kind, option[1]
=option[0]
=f.fields_for :possible_answers do |c|
%p
=c.text_field :title, class: "form-control", placeholder: "Enter a possible answer"
.actions
= f.submit 'Save', class: "btn btn-primary"
- x = f
= x.hidden_field :kind, :value => nil
= x.submit 'Corrupted Sumbit Button', class: "btn btn-primary"
It seems that there is no difference if I submit x or f, :kind is blank even I choose it in radio button and click on f.submit button. How to work this out?
I try to add multipart upload for Carrierwave in my form, but I have two controllers(admin_posts and posts) in one model(post).So I do not understand how to specify this
_form.html.haml
= form_for [:admin, #post] do |f|
= f.fields_for :photos do |photo_fields|
= photo_fields.file_field :image
= f.text_field :title, class: "form-control", placeholder: "Title"
= f.text_area :body, rows: 12, class: "form-control", placeholder: "Body"
.pull-right
= f.submit "Send", class: "btn btn-success"
how fix?
sorry for my English
Try this
= form_for [:admin, #post], url: your_action_path(#post), html: { multipart: true } do |f|
Want to show different job categories for my jobs board , using the simple form gem I have added the following to my jobs form.
_form.html.erb
<%= simple_form_for(#job, html: { class: 'form-horizontal' }) do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>
<%= f.input :title, label: "Job Title", input_html: { class: "form-control" } %>
<%= f.input :description, label: "Job Description", input_html: { class: "form-control" } %>
<%= f.input :company, label: "Your Company", input_html: {class: "form-control" } %>
<%= f.input :url, label: "Link to Job", input_html: { class: "form-control" } %>
<br/>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
but when i go to jobs.new.html it generates the following error
NameError in Jobs#new
Showing /Users/neilpatel/Desktop/Rails/jobs_board/app/views/jobs/_form.html.erb where line #3 raised:
uninitialized constant ActionView::CompiledTemplates::Category
<%= simple_form_for(#job, html: { class: 'form-horizontal' }) do |f| %>
**<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>** -<error
<%= f.input :title, label: "Job Title", input_html: { class: "form-control" } %>
Error specifies you don't have Category Model in your application. That's why rails considering Category as constant and throwing this error uninitialized constant. Try add Category Model in you app/models directory.
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>
Category.all Should be Modelname.all
So I have this form written in haml
= form_tag assign_photographers_path, class: "col-md-12 assign-photographers-form-#{portfolio.id}" do
= hidden_field_tag :portfolio_id, portfolio.id
= hidden_field_tag :user_id_1, "", class: "user_id_1"
= hidden_field_tag :user_id_2, "", class: "user_id_2"
= label_tag :user, "Begin typing to assign the first photographer:"
%br
.col-md-8.col-md-offset-2
= text_field_tag :user, nil, class: "form-control photographer-search", data_id: portfolio.id
.clearfix
%div{class: "photographer-list#{portfolio.id} col-md-6 col-md-offset-3"}
%h3 Chosen photographers
.clearfix
%br
= submit_tag "Assign Photographers", :class => "btn btn-primary"
In the source code it shows
<form accept-charset="UTF-8" action="/photographers/assign" class="col-md-12 assign-photographers-form-19" method="post"></form>
The contents of the form are then printed below the form. I have no idea why it's printing outside of the form tags. What's wrong with the code and how can I fix it?