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);
}
)}
)};
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 trying to use ajax to copy the value from a field, after the user has selected an option.
Right now I'm not using ajax and my code is something like this:
<div class="row">
<div class="form-group col-md-5">
<%= f.label :inscription_end, "End Date" %>
<div class="input-group datetime">
<%= f.text_field :inscription_end, :value => #ofer.inscription_end.blank? ? nil : l(#ofer.inscription_end, :format => :long), :"data-date-before" => "inscription_begin", :id => "inscription_end", :title => tooltip_error_title(#ofer, :inscription_end), class: "form-control", disabled: disabled_it_result_ended %>it
<span class="input-group-addon"><%= glyph(:calendar) %></span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-5">
<%= f.label :date_documents_sending, "End date to receive documents" %>
<div class="input-group datetime">
<%= f.text_field :date_documents_sending, :value => #ofer.inscription_end %>
</div>
</div>
</div>
Is there a way to automatically send the input from :inscription_end, to :date_documents_sending as soon as the user selects the :inscription_end from the calendar?
All this does is populate the date.documents_sending with the selected value
Using Jquery "ensure your elements are correct".
$(".input-group datetime").change(function (){
var selectedDate = $(".input-group datetime option:selected").val();
$(".date_documents_sending").html(selectedDate);
)};
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 this code in my _form.html.erb file:
<%= f.fields_for :request_attachments do |ra| %>
<div class="row westmontTextMuseo3" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= ra.file_field :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
<%= ra.hidden_field :file_cache %>
</label>
<%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
</div>
</div>
<% end %>
I can see why the Upload Files button appears. It seems like I want it outside the scope of the nested form, but I don't know how to do that.
My goal is to place the button once after it has provided links to each of the already attached files.
Any help would be great, thanks.
Edit: Other code to see if we can get it to work.
<%= f.fields_for :request_attachments do |ra| %>
<%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
<% end %>
<div class="row westmontTextMuseo3" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= #request.request_attachments.file_field :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
<%= #request.request_attachments.hidden_field :file_cache %>
</label>
</div>
</div>
But this fails with an undefined method file_field' for #<RequestAttachment...
This seemed to have worked
<%= f.fields_for :request_attachments do |ra| %>
<div class="row">
<div class="col-xs-12">
<%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
</div>
</div>
<% end %>
<div class="row westmontTextMuseo3" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= file_field_tag :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
<%= hidden_field_tag :file_cache %>
</label>
</div>
</div>
I assumed since the attributes were nested it wouldn't work, but it did work.
I am trying to initiate a variable showName when the page loads. It worked fine when I did it using just angular but when I implemented it with rails(with embedded ruby tags) I am not able to hide the text field and save button as my variable gets undefined. I have tried couple of ways and ended with no result.
Here is my code. Any help is really appreciated. Thanks.
Edit.html.erb:
<div class="row" ng-controller="EditUserCtrl">
<div class="col-md-6 col-md-offset-3" id="editForm">
<%= form_for(#user) do |f| %>
<h1 id="editHeader">Account Information </h1>
<%= render 'shared/error_messages' %>
<div class="form-group">
<%= f.label :name, "Full Name:" %><br>
<div class="row">
<div class="col-sm-7">
<div ng-hide="showName">
<%= f.label :name, #user.name , id:"editFields" %>
</div>
<%= f.text_field :name, { :'ng-show' => 'showName'}%>
</div>
<div class="col-sm-5" ng-show="showName">
<%= f.button "Save", :action => "update_name" , :method => :put, class: "btn btn-primary", :'ng-click' => "clicked()", id:"editAccount" %>
</div>
<div class="col-sm-5" ng-hide="showName">
<i id="nav-cart-count" class="glyphicon glyphicon-edit"></i> Edit
</div>
</div>
</div>
<% end %>
</div>
Use ng-init to initialize the variable
<div class="row" ng-controller="EditUserCtrl" ng-init="showName = true">