Filter selection in Simple Form - ruby-on-rails

I have this code in my form :
guidances/new.html.erb
<%= f.association :employee_department, as: :select %>
<%= f.input :guidance_supervisor do %>
<%= f.select :guidance_supervisor,
Employee.where('guidance_supervisor' => true).map(&:full_name) %>
<% end %>
and when selecting a department I need to make the employees who belongs to that department appears, I'm using simple_form , what is the way to make this happen ?

For this you can use dynamic drop-down concept. You can see the following post for dynamic drop down in rails:
Dynamic drop down in rails.

Related

How to display only unchecked items in active admin for has many relationship?

I am using Ruby on Rails 5 with active admin as a backend for resources management. I need to show the only unchecked items for a check_boxes field all the time in new and edit action. Instead of running a complex query for the collection I think this is the best way to manage. All of my associated models stuff related to this are working fine.
It should show only 2nd item if it is not checked already.
Right now my code snippet is
f.input :subscribers, :as => :check_boxes, :collection => Subscriber.all.collect {|subscriber| [subscriber.email, subscriber.id]}
Is there any way in active admin to display only unchecked values ?
Have you considered using collection_check_boxes for this case?
It would look something like this:
<%= f.collection_check_boxes(:subscribers_ids, Subscriber.all, :id, :email) do |b| %>
<% if !b.check_box.include?(checked="checked") %>
<%= b.label %>
<%= b.check_box %>
<% end %>
<% end %>
I think that should solve your problem. If you want to learn more about collection_check_boxes

Ruby on Rails 2 Drop Down Menus Want Validation of Selection Pre-Controller

I have a ruby on rails app that has a form. I was wondering if there is a way to make sure that a user has selected drop down menu items in both of the drop downs on this form before it is submitted and params are generated. Ideally I would like to throw and error warning them to pick 1 item on each of the drop downs and re-render the form.
The form is below:
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(#products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(#products, 'id', 'name') %>
<%= f.submit %>
<% end %>
Please let me know how I can accomplish what I stated above.
SIDENOTE: I also implemented Select2 to make the form look nicer but could not find out of there is a quick validation trick in Select2 to accomplish what I said above, if there is a suggestion for that I can post the Select2 version,
Try this:
<%= select_tag :product_id1, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select...",:required=>true %>
<%= select_tag :product_id2, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select..." ,:required=>true %>

Ransack search by a selected column dynamicaly

i want to do a search with Ransack in a User model, but i need a select with the attributes of the model as options to perform the search for that model. So if i have as attributes name, last_name, email the select should have this fields to be selected and next to it an input field to write the text that i want to search in the selected column or attribute for the User model.
Is there a way to do it with Ransack in Rails 4.1?
I think that could be something like this
<%= search_form_for #q do |f| %>
<%= f.select :selected_field, collection_of_attributes %>
<%= f.search_field :user_selected_field_cont %>
<%= f.submit %>
<% end %>
Any help will be appreciated :)
Try using ransack's attribute_select. http://railscasts.com/episodes/370-ransack?view=asciicast

How to use checkboxes using simpleform

My code is as below :
<% for market_language in #pyr_market_languages %>
<%= f.input :"pyr_crm_call_script[market_language_ids][]", :as => :check_box, input_html:{value: market_language.id}, #callscript.market_languages.include?(market_language) %>
<%= market_language.name %><br/>
<% end %>
I just want to use simple form tag for checkbox like f.input,because i couldn't see the errors for checkbox field even if it is mandatory.
Can any one of you provide the way of using checkbox in a simple form in rails.

Rails 3 Nested Form and jQuery UI datepicker updates first element only

This isn't working as I'd like.
I have an entry form for a new job, which is made up of a number of steps. By default there are 4 new and unsaved steps populated per job.
Using the code from the view below, I am able to attach the jQuery UI datepicker control to the text fields and they appear against the correct input field when i click them.
However, when I select a date from the picker it is only ever going into the field representing the start date of the first step.
<%= form_for #job, :url => jobs_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<% #job.steps.each do |step| %>
<%= fields_for "job[step_attributes][]", step do |s| %>
<%= s.label :name %>
<%= s.text_field :name %>
<%= s.label :start_date %>
<%= s.text_field :start_date, :class => :datepicker %>
<%= end %>
<% end %>
<script type="text/javascript">
$(function() {
$('.datepicker').datepicker({ dateFormat: "dd/mm/yy"});
});
</script>
I know ultimately this has to do with the generated id attribute for the input elements being the same, was wondering, has anybody successfully overcome this issue?
This fiddle: http://jsfiddle.net/twilson/u9m9L/ demonstrates my problem.
First, invoke fields_for on your form builder instead:
<%= f.fields_for :steps do |s| %>
Second, you don't need to iterate through #job.steps if you specify the association name to fields_for. There's an example on how to use it with one-to-many assoiciations here.
If you still have this problem, paste your generated HTML, it would easier to find the cause.
EDIT
OK so the problem is because Rails form builder does not generate child indexes to give unique IDs to nested fieldsets. This most likely happened because the associations are built but not yet saved to the DB. One way I see is to assign child indexes manually, like so:
<% #job.steps.each_with_index do |step,i| %>
<%= f.fields_for :steps, step, :child_index => i do |s| %>
See if that helps.
This is a somewhat contrived scenario given that you have only new objects. To be able to dynamically add/remove nested items, this would be a bit trickier. You can see how this can be dealt with in the nested model Railscasts.

Resources