Displaying currency symbols in form dropdown menu - ruby-on-rails

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

Related

Adding custom field in rails form helper

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 .

Rails 4 + ActiveAdmin: Attribute limited to a few values -- customizing ActiveAdmin form based on this?

So I have a CareerEntry model that has a fullintern attribute, which is a string that is supposed to specify whether the entry represents an internship or a full-time position. I limit the values that can appear in this attribute as follows:
validates_inclusion_of :fullintern, :in => ["Internship", "Full-time"]
However, in ActiveAdmin, the part in the edit form that deals with the fullintern attribute still has a text field. How do I make it a dropdown box where the admin can select either "Internship" or "Full-time"?
Thanks.
You can use Formtastic's input helpers to use a select input:
form do |f|
f.inputs "Details" do
f.input :fullintern, as: :select, collection: ["Internship", "Full-time"]
end
f.actions
end
See Formtastic's usage section for the full set of native capabilities.

Active admin shows html code

I'm using ejholmes Active_admin Editor and it's working fine but in my active admin index I see the html code with tags and not the correct output. I do use .html_safe in my views but here's my active admin's posts.rb
...
index.do
column :id
column "Titolo", :title
column "Corpo news", :body
column "Creato il", :created_at
column "Modificato il", :updated_at
column :link
column "Link Immagine", :image_url
bool_column :attivo
default_actions
end
...
I want to have my "Corpo news" column rendered.
Thanks for the help!
I guess you need to use the method .to_html as you can see by the source
So in the view you need to call
body.to_html

Cannot retrieve lookup values correctly

I've built a lookup table in my Rails application.
It is a table that stores lookup text values for drop-down pickers or possibly check boxes. I want the lookup table to have a view associated so the values can be easily edited. I also may want to share lookup values among multiple models for a single field in the future.
So far I've managed to get it to work for the selection of values, but then displaying the text value again on a show or index view has been problematic.
This is how I built the lookup table
rails g scaffold Lookup field_name lookup_text table_name note
In the edit.html.erb where there is a lookup on a field, I've got code like this, which works and allows me to pick from a list.
<div class="field">
<%= f.label :status %><br />
<%= f.collection_select :status, Lookup.find(:all,:conditions => ["table_name = 'course' and field_name = 'status'"]), :id, :lookup_text, include_blank: true,:prompt => "Status" %>
</div>
That all works fine. When I try to display it back I cannot find the correct syntax. The best I have found is this:
(in the controller)
#status = Lookup.where(:id => #course.status).pluck(:lookup_text)
(in the view)
<p>
<b>Status:</b>
<%= #status %>
</p>
I think I am getting the entire object. It displays like this:
Status: ["Active"]
My questions are:
(1) How do I display the value only?
(2) Is this the best approach?
I've had a look at these and other SO questions, but none are really what I am looking for:
Rails Polymorphic with Lookup Table
Implementing a lookup table in Rails
EDIT
OK this works, but it doesn't look like it is the correct solution. Has anyone got a better way of doing this?
#status = Lookup.where(:id => #course.status).pluck(:lookup_text)[0]
Just another way to show the value is #status = Lookup.find(#course.status).lookup_text
Why not to try use classes for different lookups:
class CourseStatus < ActiveRecord::Base
set_table_name "lookups"
default_scope where("table_name = 'course' and field_name = 'status'")
end
class Course
belongs_to :course_status
end
You then can use:
CourseStatus.all # e.g. to fill select options
Course.first.course_status.lookup_text # => "Active" or smth else
Or without classes:
class Lookup
def self._by_table_and_field(table, field)
['table_name = ? and field_name = ?', table, field]
end
scope :by_table_and_field, lambda { |table, field|
where(Lookup._by_table_and_field(table, field))
}
end
class Course
belongs_to :status, class_name: 'Lookup', conditions: Lookup._by_table_and_field('course', 'status')
end
Lookup.by_table_and_field('course', 'status').all
Course.first.status.lookup_text

ruby on rails how to use FormOptionHelpers to create dynamic drop down list

I have checked some tutorials but I got confused by the parameters in this method
collection_select (object, attribute, collection, value_method, text_method, options = {}, html_options ={})
I have a map model includes: :area, :system, :file
and I want to read :area from database to a drop down list, and let user choose one
I already did #map = Map.all in the view
what the method should be?
especially the parameter "attribute". In a lot tutorials, people put "id" here. But I don't know what "id" is, and in my situation I don't need any other value, just the "area".
Im not exactly sure what you are asking here but if you are trying to make a dropdown selection for use in an html form will this example help you at all?
<% nations = {'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico' => 'Mexico', 'United Kingdom'=> 'UK'} %>
<% list = nations.sort %>
<%= f.select :country, list, %>
Here nations is a hash of countries then list becomes the sorted copy of that hash. An html dropdown is then created as a part of the form "f". ":country" is the part of the model that the data is connected to while list is the options to populate the dropdown with
It's not clear from your question what the model is that's being populated with the area.
Typically, collection_select is used between related models.
eg.
class Category < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :category
end
When selecting the 'category' for a product, your view would have something like:
<%= f.collection_select(:category_id, :id, Category.all, :name, include_blank: true) %>
What this does is specify the Product.category_id as the attribute being populated with the value of Category.id. The values come from the Category.all collection, and with Category.name being the item displayed in the select. The last (optional) parameter says to include a blank entry.
Something like the following is probably what you need:
<%= f.collection_select(:map_id, :id, #map, :area) %>
However, if the model you're trying to populate has an area attribute (instead of an ID linking to the map), you might need to use:
<%= f.collection_select(:area, :area, #map, :area) %>
This specifies that the area attribute of the receiving table will be populated with Map's area attribute, which is also being used as the "description" in the select.

Resources