Maybe this question is similar to others but I seriously can't find any solutions looking at others questions answers
, I have a form in my view
<%= form_tag list_campaigns_path, :method => 'get', :id => "country_search" ,remote: true do %>
<%= select_tag "search_by_country", options_for_select(#country.collect { |country|
[country.name] }, params[:search_by_country]),{ prompt: "Select country" ,id: "select_country"} %>
<%= submit_tag "search", class: "btn grn btn-yel"%>
<% end %>
what I'm trying to do is that when someone selects a country from dropdown say he selects US then after hitting on search button that selected country will be listed below with other search results that this is the country with you have performed your search.
just a info:- i'm using country_select gem for getting countries list in dropdown
To display the selected value, you have to use:
:selected => :search_by_country
in your options_for_select.
So, change your select_tag to this:
<%= select_tag(:search_by_country, options_for_select(#country.collect { |country|
[country.name] }, selected: params[:search_by_country]), { prompt: "Select country", id: "select_country" })%>
Related
I have a search_form_for and I need to be able to select multiple Payment statuses to make a Ransack search.
We already have an f.select dropdown box for this attribute, but we now need to be able to select more than one status at the same time.
form:
<%= search_form_for #search, url: admin_payments_path, html: {class: "form-inline"} do |f| %>
<div class="form-group">
<%= f.select :status_eq, payment_status_selector, { include_blank: "Payment status.."}, class: "form-control gray" %>
</div>
<% end %>
I've tried:
<%= f.select :status_eq, payment_status_selector, {include_blank: false}, {multiple: true, as: :radio_buttons} %>
gives me a select box with all the options allowing me to select multiple, but they are not radio_buttons and an empty value "" is passed along with the selected options.
<% payment_status_selector.each do |status| %>
<%= radio_button_tag :status_eq, "#{status[1]}", false, class: 'radio-checkbox' %>
<%= label_tag( "#{status[0]}") %>
<% end %>
This gives me a radio_button for each possible status, but I can't select more than one.
I can't figure out the correct way to do it. Maybe with check_boxes are a better option to be able to select/unselect statuses?
Yes, using check_box will be a better option.
Try,
<% payment_status_selector.each do |status| %>
<%= f.check_box :payment_statuses, { multiple: true }, status, false %>
<%= label status %>
<% end %>
And you can expect the values in controller from params as:
{ search: { payment_statuses: ["status 1", "status 2"] }
make sure that payment_status_selector returns array of values.
Hope that helped.
Reference: https://medium.com/programming-problem-solving-logbook/rails-multiple-checkboxes-e9c4c7fda356
I am trying to create a select tag in a form_for where I can select multiple categories from the options. I have looked at the Rails documentation and this SO, but neither of them seem to work. So far, I have this:
<select class="selectpicker" data-style="form-control" multiple title="Choose Department(s)" data-size="5">
<%= options_from_collection_for_select(Category.all, :id, :name)%>
</select>
And my form_for looks like this:
<%= form_for(#listing, :html => {class: "form-horizontal" , role: "form"}) do |f| %>
My listings can have many categories. How am I supposed to make this save to my form? Right now, the categories aren't saving when I submit my form.
It's not working because your select isn't scoped to your #listing object. Try:
<%= f.collection_select(:category_id, Category.all, :id, :name) %>
To address #ddubs's comment suggesting to replace the select tag with a Rails form helper as well as keeping your custom HTML data attributes:
<%= f.collection_select(:category_ids, Category.all, :id, :name, {}, class: "selectpicker", title: "Choose Department(s)", multiple: true, data: { style: "form-control", size: "5" }) %>
For more information on collection_select options, look at the Rails api.
Final answer ended up being <%= f.collection_select(:category_ids, Category.all, :id, :name,{:"data-style" => "form-control", :"data-size" => "5"}, {class: "selectpicker", title: "Choose Department(s)", multiple: true}) %> as mmichael pointed out.
I'm making a little form and what I'm trying to do is to make one of the text fields get the value only when notification is created. I have:
<%= f.label :User_ID %>
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
My problem is that when other users are editing notifications, then this text field is automatically changing it's value. I'm not sure how to make it work. Thanks!
In your view you could check if the user id is already present and if so, don't display the user id text_field:
<% unless user_id.exists? %> #only if user_id is not present, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
Another option would be to add a boolean value to the model you are referring. For example, a notification model:
notification: string
edit: boolean (default: true)
After a user creates a notification, you set the boolean value for "edit" to false with an after create for example.
The next time a user edits that notification, you do the same in the view as before:
<% unless #notification.edit? %> #only if edit is set to true, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
It is a little vague but it gives you an idea how to do it.
i have created a dropdown in my application using the following code:
<% form_for :categories, :url=> {:controller => 'products', :action => 'cat'} do |f| %>
<%= f.select(:category , Categories.all.map{ |u| [u.name, u.id] }, :prompt => "Select a category") %>
<input type="submit" value="go"/>
<%end%>
controller:
def cat
#products = Product.search_category params[:category]
end
it is returning 'nil' as the parameter when i select any category from dropdown. what could be the reason?
You should expect the category id to be in params[:categories][:category] because that's how the form was set up. If you want to use params[:category], pass a name option to select
<%= f.select :category, Categories.all.map{ |u| [u.name, u.id] }, { :prompt => "Select a category" }, { name: 'category' } %>
That aside, I find it better to use collection_select when you're dealing with an ActiveRecord table
<%= f.collection_select :category, Categories.all, :id, :name, { prompt: 'Select a category' }, { name: 'category' } %>
I think the problem lies with your symbol used with form_for. Generally, a object is passed to form for as it will generate the proper routes.
This question is discussed here
fields_for doesnt working when form_for use symbol
and
Ruby on Rails : symbol as argument in form_for
I have two models which i want to search through my Search form
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get', :remote => true }) do |select| %>
<%= label_tag :search, "Enter Keywords Here" %>
<%= text_field_tag :search, params[:search] %>
<%= label_tag :country, "Search by Country" %>
<%= collection_select(:country, :country_id, Country.all, :id, :name, :prompt => 'Please Select') %>
<%= label_tag :difficulty, "Search by Difficulty" %>
<%= select_tag :difficulty, options_for_select([['Beginner'],['Intermediate'],['Expert']]), {:prompt => 'Please Select'} %>
<%= label_tag :preperation_time, "Search by preperation time" %>
<%= select_tag :preperation_time, options_for_select([['15..30'],['30..60'],['60..120']]), {:prompt => 'Please Select'} %><br>
<%= submit_tag "Search", :class => "searchbutton" %>
<% end %>
My understanding is that if I want to be able to search on all the columns my sql statement should look like this
SELECT column dish_name, difficulty, preparation_time FROM Recipe LEFT JOIN Country ON Recipe.dish_name = Country.name AND Recipe.difficulty = Country.name AND Recipe.preparation_time = Country.name
this may be totally wrong mind, what i would like to achieve is to be able to search by one parameter or up to all 4
Im not sure how to turn this into rails syntax, currently I have
q = "%#{params[:search]}%"
#countrysearch = Recipe.includes(:country).where("dish_name LIKE ? OR countries.name LIKE ? OR difficulty LIKE ? OR preperation_time LIKE?", q, q, q,q )
If anyone can point me on the right direction that would be much appreciated
Undestanding
If you want to understand better what you are doing with this methods call, you can always use the to_sql in order to print the generated SQL Query.
Also you can check in the log degenrated by rails and in the console.
Left Join Example
#result = Recipe.joins("LEFT OUTER JOIN countries ON countries.id = recipes.country_id").select
but is also possible, and better to do how you are doing
#result = Recipe.includes(:country)
Printing the query
puts #result.to_sql
Building the query
countrysearch = Recipe.includes(:country).arel_table
countrysearch = countrysearch.or(countrysearch[:country].eq(params[:search][:country])) if params[:search].has_key?(:country)
#...
countrysearch = countrysearch.or(countrysearch[:difficulty].eq(params[:search][:difficulty])) if params[:search].has_key?(:difficulty)
References
Please check for the alternatives in the following references:
rails/arel
215: Advanced Queries in Rails 3
Hope this helps you!!