Show Shipping Price When Selected In Form - ruby-on-rails

I have a Shipping_method model. A shipping method has an attribute price_in_cents.
When user is viewing the cart form i want them to be able to select which shipping method they want. Which they can.
However. I want the price_in_cents to be displayed next to each shipping method. How do i do that?
<%= simple_form_for :cart, url: user_carts_path(current_user), method: :post do |f|%>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :product_id, value: product.id %>
<%= f.hidden_field :outfit_id, value: params[:outfit_id] %>
<%= f.hidden_field :price, value: product.price %>
<%= f.input :quantity, collection: 1..10, selected: 1 %>
<%= f.input :size_id, collection: sizes %>
<%= f.input :shipping_method_id, collection: shipping %>
<%= f.button :submit, value: "Buy Now" %>
<% end %>

Try this :
<%= f.input :shipping_method_id, collection: shipping, label_method:
:price_in_cents %>

Related

Passing params to a simpleforms f.association in rails

I have many equipments that belongs to an Office
From the list of Offices, I put this link to pass the id to the form of a new equipment
<%= link_to "", new_equipment_path(office: params[:id])" %>
The generated url appears to be ok, passes the correct id of the clicked office
/equipments/new?office=6
And I check with an open cell like this and I see the number id (6 for example)
<%= f.input :observations, input_html: { value: params[:office]} %>
The problem is when I try with the real cell that is f.association, is just in blank, manually I can pick a number (1,2,3,4,5,6, etc) but It's not putting the number that I pass in the params, I try as hidden and other options and no luck
<%= f.association :office, label_method: :id, value_method: :id, input_html: { value: params[:office] } %>
#show.html.erb (offices)
<% #offices.each do |office| %>
<tr>
<td><%= office.name %></td>
<td><%= link_to "Add new equipment", new_equipment_path(office:params[:id])%></td>
</tr>
<% end %>
form.html.erb (new equipment)
<%= simple_form_for(#equipment) do |f| %>
<%= f.association :office, label_method: :id, value_method: :id, input_html: { value: params[:office] } %>
<%= f.input :observations, input_html: { value: params[:office]}%>
<% end %>
First I try without simpleforms and work without a problem
<%= form.hidden_field :office_id, value: params['office'] %>
The problem was that simpleforms does not take input_html: { value: params[:office] in f.association, I change for this and it work
<%= f.input :office_id, value_method: :id, label_method: :id, input_html: { value: params[:office] }, as: :hidden %>

Checking simple_form f.input value before submitting the form

I have this form:
<%= simple_form_for(#student) do |f| %>
<%= f.input :first_name, label: "Nombre" %>
<%= f.input :hours_studying, as: :select, collection: [1, 2],
prompt: 'Horas' %>
<%= f.input :library_id, as: :select, collection: #libraries %>
<div class="text-center">
<%= f.button :submit, "Regístrame!", class: "btn button rounded-pill
p-3 my-4 fs-18 shadow" %>
</div>
<% end %>
Libraries have a country and a city, how could I add two fields to the form to check the country and the city and filter the libraries accordingly?

Simple_form and association

I have this form:
<%= simple_form_for #request, html: {class: 'form-horizontal' } do |f| %>
<%= f.input :initiator, label: 'initiator' %>
<%= f.association :department, collection: Department.all, value_method: :id, label: 'Назначить на отдел' %>
<%= f.association :user, collection: User.all, label_method: :email, label: 'Ответственный' %>
<%= f.input :comment, label: 'comment' %>
<%= f.input :sla, label: 'SLA' %>
<%= f.button :submit, label: 'Создать', class: "btn btn-primary" %>
<% end %>
How can I make the association:
If I choose "Department 1" from: Department, the choice of the :user will only users who belong to this department. (When you open the drop-down list were only people from the Department 1, not Users.all) What parameters I have to pass the rails?
I doubt if it is possible only using simple_form
Try http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/
I would advice against loading all users in DOM, use an ajax action to filter users if they are many

How to submit a form using simple_form_for when what's being submitted isn't part of the model

I'm trying to submit a form that contains fields for both an event and an invitation to that event. Here is my form:
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :start_at %>
<%= f.input :end_at %>
<%= f.input :all_day %>
<%= f.hidden_field :owner_id, value: current_user.id %>
<% if false %>
<%= f.association :sitter, label_method: lambda { |s| "#{s.name}" }, collection: User.all %>
<%= f.association :group, label_method: lambda { |g| "#{g.group_name}" }, collection: Group.where(:owner_id => current_user.id) %>
<% end %>
<%= f.input :user_emails, as: :text %>
<%= form_tag event_invitations_path, :method => :post do %>_tag :user_emails %>
</div><div>
<%= label_tag "Your message:" %>
</div><div>
<%= text_area_tag :email_message %>
<% end %>
</div>
</br>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
This is (perhaps obviously) not working. :user_emails is NOT part of the event or invitation model as it's a list of emails that will be used to create invitations. Basically I merged two forms, one that was accepting the email/send invitation piece and one that was accepting the event information. I think I have my controller and model set up properly to take care of this but how do i submit this information as part of the same form without getting an "undefined_method" error (since user_emails doesn't belong to events). Let me know if you want to see my model/controller.
Use FormTagHelper
<%= label_tag "User", "Email"%>
<%= text_area_tag "user_emails", "example#example.com"%>
See the documentation for more options on text_area_tag and label_tag

Select element, simple_form helper

I am trying to create an element in my form that uses simple form+bootstrap. The idea is to allow a user select a type of currency from a drop down.
Customer_currency to select from either USD- US Dollars, LRD - Liberian dollar among others.
I have used the following in my form
However, it is not working, all I see is a drop down (out of position) in my form with the options but no label.
How can i create a good select element with a label using simple form
<%= simple_form_for #customer, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
<%= f.input :payment_terms %>
<%= f.input :billing_address %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :mobile %>
<%= f.input :email %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
customers_path, :class => 'btn' %>
</div>
<% end %>
<%= f.input :customer_currency, :collection => [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
Add label_method and value_method to your select, means change:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
to:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>
Update: other solution
<%= f.input :customer_currency, as: :select, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>

Resources