How to get f.select for static hash value rails? - ruby-on-rails

i have static hash :
STATUS_BRAND = {
active: "hoatdong",
deactive: "khonghoatdong",
}.freeze
my view:
= f.select :status, Brand::STATUS_BRAND, class: "form-control post-type input-bg"
i wanto display value in here is : "hoatdong" and "khonghotdong" in views

If you do not want the select to return the keys to Rails, then you have to invert the Hash:
f.select(:status, options_for_select(Brand::STATUS_BRAND.invert), ...)
Output:
<option value="active">hoatdong</option>
<option value="deactive">khonghoatdong</option>
You can look at the documentation for options_for_select for more examples.

Related

Rails_Admin related dropdowns

Is there a way to filter a dropdown list based on another dropdown's value ?
For example, if we have: Class and Student Models where Student have class_id; is there a way to filter the Students shown in the dropdown based on the selected Class ?
EDIT
Apparently, rails_admin gem has an association relationship which I was looking for; but it doesn't work perfectly.
Given two <select> elements "Class" and "Student", with the Student list containing data-class_id attributes referencing values from the Class list (see snippet below), you can filter the "Student" dropdown based on the "Class" dropdown's value using the following vanilla-JavaScript code:
var firstSelectId = "Class";
var secondSelectId = "Student";
var data_attr = "class_id";
this.addEventListener("DOMContentLoaded", function(event) {
var firstSelect = document.getElementById(firstSelectId);
var secondSelect = document.getElementById(secondSelectId);
firstSelect.addEventListener("change", function(event) {
var value = event.target.value;
Array.prototype.forEach.call(secondSelect.options, function(item) {
item.style.display = (item.dataset[data_attr] === value) ? 'inline' : 'none';
});
var selected = secondSelect.selectedOptions[0];
if (selected && selected.dataset[data_attr] !== event.target.value) {
secondSelect.selectedIndex = -1;
}
});
firstSelect.dispatchEvent(new Event('change'));
});
<form id="myform">
Select a class and student:
<select id="Class">
<option value="1">Class1</option>
<option value="2">Class2</option>
<option value="3">Class3</option>
</select>
<select id="Student">
<option value="StudentA" data-class_id="1">A</option>
<option value="StudentB" data-class_id="2">B</option>
<option value="StudentC" data-class_id="3">C</option>
<option value="StudentD" data-class_id="2">D</option>
<option value="StudentE" data-class_id="1">E</option>
<option value="StudentF" data-class_id="1">F</option>
</select>
</form>
If you can use Javascript, this Railscast will help you:
In this instance, your dropdowns may look like this:
<%= f.collection_select :class_id, Class.all, :id, :name, {:prompt => "Select a Class"}, {:id => "class"} %>
<%= f.collection_select :student_id, Student.all, :id, :name, {:prompt => "Select a Student"}, {:id => "student"} %>
And you would use Javascript to change the options in the Student dropdown. You can get the value of the class using:
class_id = $("#class").find(":selected").text()
This is the related link: https://github.com/sferik/rails_admin/wiki/Associations-scoping which edited the origin question

Select option required Ruby Rails

I have a mix of ruby rails code
I have a form with a selection option that i want to be a required, and i want to validate. If user do not select anything i want to validade with a error message.
However, I try to copy past code from internet, I'm new at ruby rails and I still not have a error message.
I also check that i use 'required' , or if i use 'validates_presence_of' doesn't make difference because it's a submit form (i think)
test_filteR_form.rb
class TestFilterForm < ApplicationForm
attribute :model, String
validates_presence_of :model
end
.html.erb
<%= f.input :fill_form_error_message,:as => :hidden, :input_html => { :value =>I18n.t('test.fill_form_error') } %>
<%= f.input :model, label: I18n.t('test.filters.model'), autofocus: true, input_html: {class: 'input-xlarge chosen-select' }, collection: TestType.options_for_select, include_blank: true %>
"/>
controller
def paginate
#test_form = TestForm.new(params)
unless #test_form.valid?
#model = params[:test_filter_form][:model]
#h_model = #model.pluralize + 'H'
#history, _query, #test_fields = TestQueryService.search!(params)
session[:test_query] = _query
session[:test_klass] = #model
else
format.json { render :json => { :error => #test_form.errors.full_messages }, :status => 422 }
end
js.coffee
$contentDiv.on 'ajax:error', 'form[data-value]', (event, xhr, status, error) ->
data = $.parseJSON(xhr.responseText)
$result = $(#).parents('tr').find('[data-result]')
controller.resultUpdateError xhr.status, $result.data('result'), data, $(#)
# Hide row loading spinner
$(#).parents('tr').find('span[role="result_update_spinner"]').hide()
# Hide saved form
$(#).parents('tr').find('.saved_form').hide()
resultUpdated: (result, data, $form) ->
if data.flash != undefined
# Sets a sucess message on page top
flash data.flash.type, data.flash.message
# Sets a success message on row
$fieldForm = $form.parents('tr').find(".messages")
$fieldForm.find('.controls').empty()
$fieldForm.find('.control-group .controls').css('color', 'green').append #_inlineMessage("Gravado com sucesso")
# Hide success message after some time
setTimeout ((self) ->
->
$fieldForm.find('.control-group .controls').empty()
return
)(this), 4000
Since you are dynamically created the selection box then there must be a default value selected which is not nil so there is no change seen you can manually create selection like this:
<div class="form-group">
<%= f.label :select_user_country %><br/>
<select class="form-control select2" name="user[country_id]">
<option value="" selected disabled>Select a Country</option>
<%#countries.each do |country|%>
<option value="<%=country.id%>"><%=country.name%></option>
<%end%>
</select>
</div>

Rails: Provide default value for the disabled select helper

I have a select helper as seen below, I have disabled it and would like give "1" as default value, but couldnt find how?
<%= f.select :fuel_incl, [[t('shared.incl'),'1'],[t('shared.notInc'),'0']], {}, {class: "Select-control u-sizeFull fuel_incl", :disabled => true} %>
It is disabled so it should select 1 as default.
EDIT
I see that it works from the console;
<select class="Select-control u-sizeFull fuel_incl" disabled="disabled" name="boat[fuel_incl]" id="boat_fuel_incl">
<option selected="selected" value="1">Included</option>
<option value="0">Not Included</option></select>
but it does not send as params, when I take out disable true and select value send the form, it works..
EDIT2
anyways, I have solved it by giving only one select value as;
<%= f.select :fuel_incl,
[[t('shared.incl'),'1']],
{ selected: '1' },
{ class: "Select-control u-sizeFull fuel_incl" } %>
Add selected option:
<%= f.select :fuel_incl,
[[t('shared.incl'),'1'],[t('shared.notInc'),'0']],
{ selected: '1' },
{ class: "Select-control u-sizeFull fuel_incl", disabled: true } %>

How to display category name

i want to display the search item name in search page as heading.
(here it is displaying value instead of name)
I am searching through ransack.
My search view
<%= form_tag location_path, :method=>'get' do %>
<%= select_tag :q, options_from_collection_for_select(Category.all, :id, :name, params[:q]), :class=>"btn1 btn-default1 dropdown-button1 dropdown-toggle"%>
<input type="submit" value="SEARCH" class="btn1 home-search-button" >
<%end %>
my search controller
def location
q = params[:q]
#key = q
#property = Property.ransack(location_or_category_name_cont: q).result(distinct: true)
end
MY location.html.erb
HERE IS WHERE I WANT DISPLAY THE PROPERTY NAME as
<h1>Properties in <%=#key%></h1>
i am getting output like this.
BUT, instead of 2, I want to get output like this Properties in Residential.
I have a category table with 3 category with fields id(1 ,2,3) and category_names (residential commericial and Finance.
Any help is appreciatable
Assuming category_name is the column of your categories table:
#key = Category.find(q).category_name
use this code:
#key = Category.find_by(:id=>q)
In view:
<h1>Properties in <%= #key.category_name if #key.present?%></h1>

select_tag helper with grouped_options_for_select order

I have this in my model:
LOCATION_IN_UK = {'England' => [['Berkshire', 1],['Cambridgeshire',2],['Cheshire',3]], 'Scotland' => [['Dumfries and Galloway',4],['Fife',5],['Lothian',6]], 'Others' => [['Outside UK',7]]}
And this is in the view:
<%= select_tag :location, grouped_options_for_select(Location::LOCATION_IN_UK), :id => 'location-dropdown' %>
This code generate following html:
<select id="location-dropdown" name="location">
<optgroup label="England">
<option value="1">Berkshire</option>
<option value="2">Cambridgeshire</option>
<option value="3">Cheshire</option></optgroup>
<optgroup label="Others">
<option value="7">Outside UK</option></optgroup>
<optgroup label="Scotland">
<option value="4">Dumfries and Galloway</option>
<option value="5">Fife</option>
<option value="6">Lothian</option></optgroup>
</select>
1. How to skip alphabetical sort order. I want elements locate exactly as in the hash LOCATION_IN_UK.
2. How to insert prompt into this? :prompt => 'Please select' Doesn't work
To answer your prompt question, prompt is not a hash, it is the third parameter of the method call. So you would do:
<%= select_tag :location, grouped_options_for_select(LOCATIONS_IN_UK, nil, "Please Select"), :id => 'location-dropdown' %>
And looking at the source code, it seems there is no way to skip the sorting. You could write your own helper method though. Here is the source
# File actionpack/lib/action_view/helpers/form_options_helper.rb, line 449
def grouped_options_for_select(grouped_options, selected_key = nil, prompt = nil)
body = ''
body << content_tag(:option, prompt, { :value => "" }, true) if prompt
grouped_options = grouped_options.sort if grouped_options.is_a?(Hash)
grouped_options.each do |group|
body << content_tag(:optgroup, options_for_select(group[1], selected_key), :label => group[0])
end
body.html_safe
end
You could modify/override that method, but that may break if you are using this function elsewhere, which is why I would suggest you put the following in your application_helper.
def unsorted_grouped_options_for_select(grouped_options, selected_key = nil, prompt = nil)
body = ''
body << content_tag(:option, prompt, { :value => "" }, true) if prompt
##Remove sort
#grouped_options = grouped_options.sort if grouped_options.is_a?(Hash)
grouped_options.each do |group|
body << content_tag(:optgroup, options_for_select(group[1], selected_key), :label => group[0])
end
body.html_safe
end
You can then call unsorted_grouped_options_for_select and it should work.
<%= select_tag :location, unsorted_grouped_options_for_select(LOCATION::LOCATION_IN_UK, nil, "Please Select"), :id => 'location-dropdown' %>
I had the same problem. You can solve this by using the array version instead of hashes since it only orders it if it is_a?(Hash).
See the docs for the format: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/grouped_options_for_select

Resources