Adding custom field in rails form helper - ruby-on-rails

I'm working in a rails app and for country state and city select field I want to create a custom field so that It can be used later as already existed field.
For example
f.country_select :country
Above field should automatically generate a select with countries list.
I want three fields country, state and city so connected that when country field selected state select field gets state list and when state is selected city select works same.
Thank you for your valuable answer.

For this you should start from here . In this rails cast you can understand how to dynamically populate data in child(State) select box on basis of parent(Country) selection.
You need to extend functionality for cities same.
But in given tutorial you should have countries and state data. For managing that data you can use City-State.
In tutorial it is used like Country.find(:all), So you need to change it to CS.get. and for state of a selected country like CS.get :us.
Hope this works.

Yes, you can add this method to ActionView::Helpers::FormBuilder class
here is example for
class ActionView::Helpers::FormBuilder
def error_message_on(method, options = {})
#template.error_message_on(#object_name, method, objectify_options(options))
end
end
and then you can do this
= f.label :description, I18n.t(:description)
= f.text_field :description, class: 'form-control'
= f.error_message_on :description

You can use the gem city-state github gem link .

Related

Displaying currency symbols in form dropdown menu

I have a form in a commerce application where users can add an item listing.
In this create#item form, I'd like the user to be able to select (from a dropdown menu) what currency their pricing is in.
I've created a Currency model, views and a controller so the admin can add currency types. (I want the admin to be able to limit currency types).
Here is the currency migration file:
class CreateCurrencies < ActiveRecord::Migration
def change
create_table :currencies do |t|
t.string :name
t.string :symbol
t.timestamps null: false
end
end
end
(The "symbol" being a string that holds the currency HTML code)
I connected Currency and Item with a belongs_to/has_many relationship in the db. I then implemented a dropdown menu in the create#item form where users can select the currency.
My question is, how can I display the currency symbol in the dropdown menu?
Here's what I tried.
<%= f.collection_select :currency, Currency.order(:name),:id, "#{:symbol}".html_safe %>
The problem is, this doesn't display the currency symbols as I would have hoped; it just displays the string that was entered (the currency HTML code).
For example, with the code as it is, if an Admin entered the currency HTML code for $ (&#36), the dropdown shows "&#36" isntead of the expected "$")
Thanks in advance!!
Quick answer is: use raw method to unescape the html code.
I've just reproduced your code on my machine and noted a strange behavior.
For some reason raw doesn't work with collection_select and I can't figure why. Consider using select helper and 'manually' iterate your collection. Here is two identical variants:
= form_for "test" do |f|
%p collection_select variant
= f.collection_select :currency, User.all, :id, raw(:symbol.to_s)
%p select variant (works!)
= f.select("currency", User.all.collect {|u| [ raw(u.symbol), u.id ] })
You can use the HTMLEntities gem. I would recommend setting it up as a helper method which you can use in the view.
helper file
def currency_symbol(currency_code)
HTMLEntities.new.decode(currency_code)
end

How to set-up Rails form select for a collection

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"}

How to send two values with a rails collection_select

I want to send two values with my collection select. Currently, i'm saving the ID but I want to have access to the name in my controller as well.
= f.collection_select :foo_id, #foos, :id, :name
That's my code, pretty simple. Just having trouble getting access to that name.
The collection also comes from a external API, so I don't want to have to touch the API again to get the name.
As, I said in the comment. You can customize the <option> tag values like :
= f.collection_select :foo_id, #foos, ->(ob) { "#{ob.name}|#{ob.id}" }, :name
Now, inside the controller just split it the value on |, and use it.
This is just an idea out of millions.

rails4-autocomplete. Getting association name in autocomplete field

I'm trying to add an auto complete field using https://github.com/peterwillcn/rails4-autocomplete
I have a Topic model that belongs to a City. City belongs to Country. i.e. city = London. then city.country.name = England.
I can get my autocomplete to display the city name with
#controller
class TripsController < ApplicationController
autocomplete :city, :name
#view
<%= f.autocomplete_field :city_id, autocomplete_city_name_trips_path %>
But I want the country name to show in the auto complete field too. i.e. London, England. not just London.
Is there a better way to do this?
EDIT: also this passes the city name to the controller, I would like it to pass the city_id
If you've got the country in another field then it is pretty easy with autocomplete. You want to use the update_elements option in your view to tell autocomplete what other fields need to be filled in.
<%= f.autocomplete_field :city,
autocomplete_department_name_incidents_path,
update_elements: {country: '#trips_country'} %>
The #trips_country in the above snippet is the ID of the field in your form that you want updated. country: is extra data that autocomplete fetches for you.
So you also need to go to your form and tell autocomplete to grab other data.
autocomplete :city, :name, extra_data: [:country]
That's what would work if everything were in the same model. I've never tried to pull data from a different model using autocomplete so I don't know if it will work. Hopefully this is enough of a push to get you there.
The rails4-autocomplete gem has the option display_value which is what you want to use.
class TripsController < ApplicationController
autocomplete :city, :name, display_value: :name_with_country
Then in the City model
def name_with_country
name + ', ' + country.name
end

Simple Drop Down List from Array in Rails

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...

Resources