Here i'm using two forms One is Percentage,other is CGPA among both onlt one should be taken and other should be not taken
Here is the code
<div class="row">
<div class="col-sm-6">
<%= form.number_field :percentage, label: "Percentage*", min:0, max:100, enable_dis%>
</div>
<div class="col-sm-6">
<%= form.number_field :marks, label: "CGPA*", step:0.01, min:0, max:10 %>
</div>
</div>
Related
Hopefully someone can help me out with this! I am trying to implement stripe elements in rails and basically what I have is an Orders class, but the person checking out has the option to choose between two forms... the first form has two additional field options for them to fill out if the click this option. The second option has two less fields to fill out in the order form. Right now I am rendering this form as a partial and I am restricting whether or not the two conditional fields are shown by passing a local to the form partial. This is all working great. What is NOT working, is that my Stripe elements tag is rendering fine on the first form render, but it never renders in a functional state or with any css styles on the second form. The stripe_elements_tag in the second rendered version of the tag is totally useless. Can anyone think of a better way to do this or a fix that might work? I tried hiding the content in the divs and displaying it with an on click, show the form, but this also did not work. The second form would never be shown ( i noticed this option also made the form animations slower :( .
At this point I am considering actually just writing two separate forms even though thats not very DRY just to see if it will work that way.
Any ideas or thoughts are very much welcomed!
Have a great day everyone!
here is my new.html.erb code
<%= stripe_javascript_tag %>
<div class="container">
<div class="mx-auto" width="400px">
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Referred By Group
</button>
</h5>
</div>
<div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<%= render "order_form", locals: { buy_method: "group" } %>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h5 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Purchasing Independently
</button>
</h5>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
<%= render "order_form", locals: { buy_method: "individual" } %>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6" id="flash-message">
<%= render partial: 'flash' %>
<% #order.errors.full_messages.each do |msg| %>
<li> * <%= msg %> </li>
<% puts msg %>
<% end %>
</div>
<div class="row">
<div class="col-md-9 mb-md-0 mb-5">
<%= form_for #order do |f| %>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :customer_name, "Your Name *" %><br />
<%= f.text_field :customer_name, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :email, "Email *" %><br />
<%= f.text_field :email, class: "form-control" %>
</div>
</div>
</div>
<% if locals[:buy_method] == "group"%>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :student_name, "Student Name *" %><br />
<%= f.text_field :student_name, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :school_name, "School or Group Name *" %><br />
<%= f.text_field :school_name, class: "form-control"%>
</div>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :street_address, "Street Address *" %><br />
<%= f.text_field :street_address, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :number_books, "Number of books to purchase *" %><br />
<%= f.select :number_books, (0..99), class: "form-control", selected: 0%>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :city, "City *" %><br />
<%= f.text_field :city, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :state, "State *" %><br />
<%= f.select :state, [ "--",'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FM', 'FL', 'GA', 'GU', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PW', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VI', 'VA', 'WA', 'WV', 'WI', 'WY' ], class: "form-control"%>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :zip_code, "Zip code *" %><br />
<%= f.text_field :zip_code, class: "form-control" %>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
<div class="checkbox">
<%= f.check_box :email_permission, checked: "checked", checked_value: true, unchecked_value: false %>
<%= f.label :email_permission, class: "checkbox-inline" %>
</div>
</div>
</div>
<div class="form-group">
<label for="card-element">Credit or debit card *</label>
<div id="card-element" class="form-control" >
<%= stripe_elements_tag submit_path: contact_index_path %>
</div>
</div>
<div class="form-group">
<%= f.label :total %>
<%= f.label :total, id: "total", value: "$#{ #order.total }" %>
</div>
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div>
<% end %>
</div>
</div>
You need to create two separate Instances of the card referencing your stripe api_key two separate times. You cannot create two elements using the same instantiation of your stripe api key.
I'm new at programming and Rails in particular.
I have a form to create new goal records.
I have a select to chose the goal "Horizon": "weekly", "quarterly", or "yearly"
I also have a radio button, "Due date" to set the goal as for this term or next term (e.g. in the case of a weekly goal: this week or next week).
What I would like to do: is to dynamically update the radio button, based on the selected option of the select.
<%= form_with(model: #goal, url: goals_path, id: "new-goal-form", local: true) do |f| %>
<p><%= #goal.errors[:date].first %></p>
<div class="field">
<label class="label">Title</label>
<div class="control">
<%= f.text_field :title, class: "input"%>
</div>
<p><%= #goal.errors[:title].first %></p>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<%= f.text_area :description, class: "textarea"%>
</div>
</div>
<div class="field">
<label class="label">Horizon</label>
<div class="control">
<div class="select">
<%= f.select :horizon, options_for_select({"Weekly" => "week", "Quarterly" => "quarter", "Yearly" => "year"}, #horizon), include_blank: true %>
</div>
</div>
</div>
<div class="field">
<label class="label">Due Date</label>
<div class="control">
<%= f.radio_button :date, DateTime.current.to_date.end_of_week.strftime('%Y-%m-%d'), :checked => true, id:"radio-this", class: "is-checkradio" %>
<label for="radio-this">
This <%= #horizon.capitalize %>
</label>
<%= f.radio_button :date, 1.week.from_now.to_date.end_of_week.strftime('%Y-%m-%d'), id:"radio-next", class: "is-checkradio" %>
<label for="radio-next">
Next <%= #horizon.capitalize %>
</label>
</div>
</div>
<div class="field">
<label class="label">Related goal</label>
<div class="control">
<div class="select">
<%= f.select :related_goal_id, options_from_collection_for_select(#related_goal, 'id', 'title'), include_blank: true %>
</div>
</div>
</div>
<br/>
<div class="field is-grouped">
<div class="control">
<%= f.submit "Save", class: "button is-primary" %>
</div>
<div class="control">
<%= link_to "Cancel", goals_path(horizon: #horizon), class: "button" %>
</div>
</div>
<% end %>
I have tried adding :onchange at the end of the select, but nothing actually happens when changing the selected option.
Many thanks in advance.
Are your radio buttons id'd differently? They seem to both have the same autogenerated id. That won't work in javascript. Down below you set a click event for the select box and depending on what child is selected, you set a radio button checked to true.
$(document).ready(function(){
//when select option is chosen, set radio button to selected.
$("#select").click(function(){
var option = $(this).children("option:selected").val()
if(option == "weekly"){
$("#radio_button_weekly").prop("checked", true);
}
)}
)};
I use the nested_form_fields gem to tranlsate some specific fields in a technical document. Translations are nested fields associated to the document, so that each document has one instance of translation per field per language.
In the _form view to edit the document, nested_form_fields is called for each translated field, and the location to where display the corresponding input is set by the id of the contaning DIV:
<div class="row">
<div class="col-md-1">
<%= image_tag("next32.png", :id => "unfold") %>
</div>
<div class="col-md-1 text-right"> <%= t('Name')%>:
</div>
<div class="col-md-10 field"><%= f.text_field :name, :class => "col-md-8" %>
</div>
</div>
<!-- Translations management -->
<div class="translation">
<div class="row">
<div class="col-md-10 col-md-offset-2" id="name_translation">
<%= f.nested_fields_for :translations do |locution| %>
<div class="row">
<div class="col-md-1">
<%= locution.collection_select :language, #other_languages, :property, :name %>
</div>
<div class="col-md-8">
<%= locution.text_field :description, :class => "col-md-10" %>
</div>
<div class="col-md-1">
<%= locution.remove_nested_fields_link {image_tag("remove.png")} %>
</div>
<div class="col-md-1">
<%= locution.hidden_field :field_name, :value => 'name' %>
</div>
</div>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-2">
<%= f.add_nested_fields_link :translations, image_tag("add.png"), data: {insert_into: "name_translation"} %> <%= t('New') %>
</div>
</div>
<br/>
</div>
<!-- End of translations -->
Using this code for 2 or 3 fields in a page, all the translated fields available to the page are displayed in each instance of the nested_fields_for method.
I was wandering if there is a way to add a filter in the f.nested_fields_for :translations (in the form of a block?), or if I have to take all the translation records and filter out in the do |locution| loop.
Thanks for your help!
The solution came from Nico Ritche, the author of the nested_form_fields gem himself:
Filtering does just work like with normal fields_for, you can supply
the filtered translations directly:
f.nested_fields_for :translations, your_filtered_translations do
|locution|
Which leads to the following update in my code:
<%= f.nested_fields_for :translations, #my_object.translations.where("field_name='name'") do |locution| %>
Thanks a lot Nico!
This is likely a super easy answer for a Ruby on Rails expert. I have a form and need to add in a checkbox that has multiple items. I've been messing with the following code for a lot longer than I'd like to admit:
<%= form_for :lead, url: something, html: {id: 'product-form'} do |f|%>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label :product%>
<%= f.check_box :product, {multiple:true}, "option", "option2", :class => 'form-control'%>
</div>
</div>
</div>
<% end %>
With this code I get the error "wrong number of arguments (5 for 1..4)".
Basically I just want someone to be able to pick multiple options. I've also tried the following:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label :product%>
<%= f.check_box :option1, "Option1" :class => 'form-control'%>
<%= f.check_box :option2, "Option2", :class => 'form-control'%>
</div>
</div>
</div>
And I get the delightful "undefined method `merge' for "Option1":String". What am I missing to put the values in associated with the label?
I use Rails 5.1.6 and this is how I achieved adding multiple options for checkbox. I also placed it in a dropdown.
In the migration file:
t.string :skills
In my controller "tasks_controller.rb":
def task_params
params.require(:task).permit(:work_type, :title, :post, {skills: []})
end
In my model "task.rb" I did:
validates :skills, presence: true
serialize :skills, JSON
I created a module mobilephones_data.rb in directory "app/model/concerns/" which holds all the options of the checkbox as an array that can be edited easily.
In app/model/concerns/mobilephones_data.rb I wrote:
module Mobilenphones_Data
Activities = [
'Amazon Kindle', 'Blackberry', 'iPad', 'iPhone', 'Mobile Phone', 'Nokia',
'Palm', 'Samsung'
]
end
This will put all data from module's array into the checkbox drop-down as checkbox options. In My form I did:
<div class="card">
<a class="card-link card-header" data-toggle="collapse" href="#collapseOne">
Mobile Phones
</a>
<div id="collapseOne" class="collapse" data-parent="#accordion">
<div class="card-body">
<% Mobilenphones_Data::Activities.each do |activity| %>
<div id="skill_list" class="col-lg-3 col-md-4 col-sm-12 col-12">
<%= f.check_box :skills, { multiple: true }, activity, false %>
<%= activity %>
</div>
<% end %>
</div>
</div>
</div> <!--bottom-->
</div>
In my view "show.html.erb":
<% #name_of_model.skills.each do |skill| %>
<%= skill %>
<% end %>
For posterity sake:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label "Select a Product" %><br />
<%= f.check_box(:option) %>
<%= f.label(:mug, "Option") %><br />
<%= f.check_box(:option2) %>
<%= f.label(:mousepad, "Option2") %>
</div>
</div>
</div>
I have used MTI (Multiple Table Inheritance) structures to create different SignUp Forms for my users such that I have 3 different partials called _company.html.erb, _individual.html.erb, and _admin.html.erb, since they have different details to be used for signing up into the app am working on. However, I want to render each partial such that I could achieve a TabbedForm based on Bootstrap.
For more clarity, I like to achieve something like this picture below:
Here is what I have done:
SignUp View
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
Login
</div>
<div class="col-xs-6">
Register
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<%= render 'companies/form' %>
<%= render 'individuals/form' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
One of the Partials rendered in the Signup View above _form.html.erb
<%= simple_form_for(Company.new) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :company_name, label: "Company Name", placeholder: 'Company Name' %>
<%= f.input :company_url, label: "Company URL", placeholder: 'Company URL' %>
<%= f.input :country, label: "Country", placeholder: 'Country' %>
<%= f.input :contact_person, label: "Contact Person", placeholder: 'Contact Person' %>
<%= f.input :phone_number, label: "Phone Number", placeholder: 'Phone Number' %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
How do I wrap my Partials in Bootstrap Tabs such that I can have a TabbedForm like the provided picture above?
try this on your sign up view file:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
Login
</div>
<div class="col-xs-6">
Register
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
Individual
</li>
<li role="presentation">
Company
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="individual">
<%= render 'individuals/form' %>
</div>
<div role="tabpanel" class="tab-pane" id="company">
<%= render 'companies/form' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#myTabs a').click(function (e) {
e.preventDefault()
$(this).tab('show')
});
</script>
NOTE: you need to have bootstrap javascript included in your view file for this to work.