How to get selected option in edit page for select menu - ruby-on-rails

This is my select tag in ruby, after submitting the form ,i want to edit the selected option. so that i done <%= render 'form' %> in my edit.htm.erb ,but the option that i submitted is not appearing in the select field, when i click edit button. it shows select a category
Please suggest me a solution to solve this
form.html.erb
<%= simple_form_for([:coaches, #programme]) do |f| %>
<%= f.input :title %>
<%= select_tag 'category', options_from_collection_for_select(#categories, 'id', 'name',#categories.category_id), :class => "wrapper-dropdown-3_1", :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>
<%= f.button :submit, "PUBLISH", :class => "btn_style" %>
edit.html.erb
<%= render 'form' %>

#categories = Category.all.map{|c| [c.name, c.id]}
<%= simple_form_for([:coaches, #programme]) do |f| %>
<%= f.input :title %>
<%= f.input :category, as: :select, collection: #categories, selected: f.object.category, input_html: { class: 'wrapper-dropdown-3_1'}, :onchange => 'update_subscategories_div(this.value)', include_blank: "Select a Category" %>
<%= f.button :submit, "PUBLISH", :class => "btn_style" %>
<% end %>

You need to pass id of selected element as third argument in options_from_collection_for_select method.
<%= select_tag 'category', options_from_collection_for_select(#categories, 'id', 'name',category_id), :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>

Related

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?

Rails : Drop down in simple_form doesn't retain value when directed to edit path

I have a drop down selector in a form and even after saving an object, when I go to my edit page, the drop down reverts to the first item. If submit is clicked, the value changes to the first item in the list.
In this case the drop down contains a list of states. Every time I go to the edit page, Alabama is selected and if I don't manually change the value back to what it initially was, the state becomes Alabama.
<%= simple_form_for #event, url: coin_event_path(#coin.id) do |f| %>
<%= f.input :content, :label => "Event Description", class: 'form-control' %>
<%= f.input :link, :label => "Link to Event", class: 'form-control' %>
<%= f.input :date, order: [:month, :day, :year], class: 'form-control' %>
<%= f.input :time, as: :time, html5: true, class: 'form-control' %>
<%= f.input :city, class: 'form-control' %>
<%= f.select :state, options_for_select(us_states),{}, class: 'form-control' %>
<%= f.input :description, :label => "Event Description", class: 'form-control' %>
<% if can? :destroy, Event %>
<%= f.select :accepted, [['Accepted', true], ['Not Accepted', false]] %>
<% end %>
<%= f.button :submit, 'Submit' %>
<%= link_to "Back", coin_path(#coin.id), class: "btn btn-default" %>
How do I change this so it stays on the state that it's supposed to?
You can using selected, like:
options_for_select(us_states, selected: "set_current_value")
More usage examples - options_for_select() docs.

Filter cities by state in Simple_form Ruby on Rails

I'm using simple_form to create my scaffolding forms in my current project. I need some help to improve my address CRUD. After my db:seed there are lots of cities registered.
My current form for address is:
<%= simple_form_for #address, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :street%>
<%= error_span(#address[:street]) %>
<%= f.input :zip%>
<%= error_span(#address[:zip]) %>
<%= f.label :city_id %>
<%= f.collection_select(:city_id , City.all, :id, :name, prompt: true) %>
<%= error_span(#address[:city_id]) %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
address_path, :class => 'btn btn-default' %>
<% end %>
How can I filter my citties collection bellow selecting first the state ?
<%= f.collection_select(:city_id, City.all, :id, :name, prompt: true) %>
Thanks
Add this class method to your model:
def self.by_state
order('state DESC')
end
Then use it in your form:
<%= f.collection_select(:city_id , City.by_state, :id, :name, prompt: true) %>

Custom parameters from view to controller

i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object #event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
Add hidden filed in your form and set its value equal to #event.id
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= hidden_field_tag :event_id, value: #event.id%> #add this
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
<% end %>
You can use now event id in your controller as params[:event_id]
<%= hidden_field_tag :event_id, #event.id %>
So this will be available in params[:event_id].
Simply pass an hidden field in form as followings
<%= hidden_field_tag :event_id, value: #event.id%>
It will available in controller as
params[:event_id]

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