I am attempting to search for any premises that contains the animal 'cattle'.
A premises can have multiple different animals.
This search will work in any scenario apart from when the animal 'cattle' is not first in the field. E.g. if I search for 'Cattle' in the dropdown I will see all premises that contain cattle, only if it is listed first in the field. E.g. one field is 'Cattle', Horse'. This premises will still be found however, if it was 'Horse', 'Cattle' the premises will not be found in the search.
View:
= hidden_field_tag(:species, #species)
= text_field_tag(:search, #search, class: 'input-large search-query', placeholder: 'Search')
Controller
#species = params[:species].presence
if #species
prem_ids = StatevetDoc.unmatched.select(:holdings_match).where(default_species: #species).limit(#lines_per_page).collect { |doc| doc.holdings_match.values }.flatten.uniq[0,999]
#prems = #prems.where(id: prem_ids)
end
Related
Suppose I have three tables:
company
id
name
cars
id
name
companies_cars
company_id
car_id
Knowing that a person can have many cars, and that there are already many cars registered in the database, how can I make a form to register a new user and check which cars he has?
This form will have an input field for the name and one check box for each registered car.
I have tried to do it in many ways and looked for an answer in the web, but nothing that I tried worked :/.
You can give him select-list with multiple selection:
= form_for #user,
url: user_path,
html: { class: 'form-class', method: :post } do |f|
= f.text_field :name,
class: 'name-class'
= f.select :cars_ids,
options_from_collection_for_select(Car.all, :id, :name),
{},
{ multiple: true,
class: 'cars-class',
data: { placeholder: 'Select one ore more...' } }
= f.submit 'Register',
class: 'button-class'
Then in params (after submission), you will receive an array of ids of selected cars.
I suppose you also should permit cars_ids key in params (I'm not using classic rails-way, so can not check).
And don't forget to create users table, appropriate model etc.
I have a search field where I want to add two dropdowns that should help me filter my search results. In my database I have one Product model with two columns, named country and type.
The first dropdown should filter by country and the second should filter by type. However, I am trying to set the second dropdown dynamically meaning that depending on the first dropdown (country) only those types should be displayed in the second dropdown that match the respective country selected. If possible this behavior should also work the other way around i.e. depending on the type, only matching countries should be displayed.
I've been trying several tutorials that work with more than one model however, I didn't manage to get it to work with only one model. My code for the dropdown selections is the following:
<%= form_tag("filter", :id => "filter_form", :method => "post") do %>
<label for="country" class="country">Country</label> <%= select_tag :country, options_for_select(Product.pluck(:country).uniq), { include_blank: 'Select country' }%>
<label for="type" class="type">Type</label>
<%= select_tag :type, options_for_select(Product.pluck(:type).uniq),{ include_blank: "Alle Kategorien" }%>
Is is possible with only one model?
Do I need AJAX for this or is Javascrip/jQuery sufficient?
Thank you very much!
I'm working on a code base that I'm not very familiar with, specifically Haml. I need to set-up a select dropdown to select a user.
I have the following code in my controller:
def edit
#franchise = Franchise.find params[:id]
#ab_reps = User.where role: "admin-ab"
authorize! :update, #franchise
end
I have the following code in my form (that doesn't currently work):
= f.select :ab_rep, options_for_select(#ab_reps, f.object.ab_rep), {prompt: "AB Representative"}, {label: false, right_class: "col-sm-10", class: "ab-rep-field"}
Couple questions:
1.) #ab_reps is an array of user objects. I have the following method in my user model:
def name
[first_name, last_name].compact.join(" ")
end
How do I get the select to display the user names instead of the user objects (which it currently does) ?
2.) Is my current set-up even close to being correct?
Thanks for your help!
You are close, you need to provide the methods for the option value and the option text, as well as the collection which in your case is #ab_reps. Additionally you can provide a hash for prompts and for html_options such as class names, which you've done.
Rails has a few different helpers you can use for select tags including options_from_collection_for_select. I've used collection_select often, http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
= f.collection_select :ab_rep, #ab_reps, :id, :name, {prompt: "AB Representative"}
There is a model named Company and it is having number of records. Later I added a field named area. I search field, i am adding this field too.
I am displaying all the areas in a drop down as follows:
<%= label_tag :area, "Area" %>
<%= select_tag 'area', options_for_select(Company.uniq.pluck(:area)),include_blank: true, class: 'form-control'} %>
Now areas are displaying fine but when I give area as "us" in one case and "US" in another case and "Us" in other case, it is displaying 3 fields
and also the previous records will be having area fields as empty/blank, here it is showing 2 or more blanks.
How to show unique records for capital and downcase and how to show only one blank in drop down?
Change this:
Company.uniq.pluck(:area)
to this:
Company.pluck(:area).compact.map(&:downcase).uniq
This will give you uniq downcased areas. i.e. you will get only us instead of three options: US, us and Us.
This seems like it should be in Rails A1, but I can't find it any where. I have a bunch of locations stored in a Location table, made up of text fields City, County, Country etc. (This should have all been normalised out in my opinion, but there you go...).
A user can search by Location, and they enter into the separate search fields. At the moment, I'd like the Country such box to be a drop down list of countries. I currently get the list of countries in the db as follows:
#countries = Location.find(:all, :select=>"DISTINCT country")
The search box is currently just a text field:
<%= f.text_field :country,{:placeholder => "Country",:size=>20 } %>
But I'd like that to be a drop down box of the countries that are already in the #countries variable. It will just pass the country as a string. I'm a complete Newbie to Rails and even the basics are flumaxing me...Help!
If name is the actual name of the county in your country model:
<%= f.select(:country, #countries.map(&:name), {:include_blank => 'Select a Country'}) %>
You could do differently, having the value of the field different from the value displayed. Documentation lives here.
FYI, I usually use a very convenient gem named Carmen to handle country lists etc...