GoogleVisualr gem, undefined method to_js - ruby-on-rails-3

I am using a gem "google_visualr" to plot some histograms. I took an example code and tried to run it but I get the following message:undefined method `to_js' for nil:NilClass
my controller:
def bar_chart
data_table = GoogleVisualr::DataTable.new
data_table.new_column('string', 'Year')
data_table.new_column('number', 'Sales')
data_table.new_column('number', 'Expenses')
data_table.add_rows(4)
data_table.set_cell(0, 0, '2004')
data_table.set_cell(0, 1, 1000)
data_table.set_cell(0, 2, 400)
data_table.set_cell(1, 0, '2005')
data_table.set_cell(1, 1, 1170)
data_table.set_cell(1, 2, 460)
data_table.set_cell(2, 0, '2006')
data_table.set_cell(2, 1, 660)
data_table.set_cell(2, 2, 1120)
data_table.set_cell(3, 0, '2007')
data_table.set_cell(3, 1, 1030)
data_table.set_cell(3, 2, 540)
opts = { :width => 400, :height => 240, :title => 'Company Performance', vAxis: {title: 'Year', titleTextStyle: {color: 'red'}} }
#chart = GoogleVisualr::Interactive::BarChart.new(data_table, opts)
end
my index.html.erb:
<script src='http://www.google.com/jsapi'></script>
<div id='chart'></div><%= render_chart #chart, 'chart' %>
Where is my mistake?
Thanks in advance

Are you hitting http://localhost:3000/[controller_name]/bar_chart?
I believe you are not and you are probably hitting the controller#show action instead,
and that is why the view cannot find #chart (nil class).

Related

TCA selectCheckBox - Output in fluid template

I use in TCA
'uebersetzungen' => [
'exclude' => true,
'label' => 'LLL:EXT:maschinen/Resources/Private/Language/locallang_db.xlf:tx_maschinen_domain_model_maschine.uebersetzungen',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'items' => [
['6:1', 1],
['8:1', 2],
['11:1', 3],
['14:1', 4],
],
'size' => 4,
'maxitems' => 1,
'eval' => ''
],
],
to get the output in fluid template with
<f:switch expression="{maschine.uebersetzungen}">
<f:case value="1">6:1</f:case>
<f:case value="2">8:1</f:case>
<f:case value="3">11:1</f:case>
<f:case value="4">14:1</f:case>
</f:switch>
Now I want to change the renderType to
'renderType' => 'selectSingleBox'
but I have no idea how to get the several values.
I read TYPO3 TCA type select in FLUID?
but I'm not sure whether it's the same problem because I have a different syntax in TCA
Thanks to #Paul Beck I found an answer for my problem.
I can use the solution posted here: TYPO3 TCA type select in FLUID?
But I had to change the line
protected $featuresDecoded;
to
protected $featuresDecoded = [];

How to filter out select options based on certain value?

My form has:
<%= f.select(:amount, Payment::AMOUNT_VALUES, {:blank => false}) %>
My model has:
AMOUNT_VALUES = { '$ 0' => 0, '$ 1' => 1, '$ 2' => 2, '$ 5' => 5, '$ 10' => 10 }.
If user select $2 and submits form. The next time he goes back to the form, I need it only to display everything above $2 in the select box. Therefore, the options should be $5 and $10 only.
How can this be achieved?
I recently wrote some helper code that helped me rule out days of the week where a store had already posted business hours for. It shouldn't be too hard to adapt to your specifications.
Updated: Added question-specific code and updated with a more-efficient reject block.
module HoursHelper
...
def generateDayStrings (unavailable_days)
all_days = [ ['Sunday', 'Sun'],
['Monday', 'Mon'],
['Tuesday', 'Tue'],
['Wednesday', 'Wed'],
['Thursday', 'Thu'],
['Friday', 'Fri'],
['Saturday', 'Sat'] ]
all_days.reject do |day|
(day - unavailable_days).size != 2
end
end
...
end
<%= f.select :day_of_week, generateDayStrings(#days_with_hours) %>
# #days_with_hours = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
# generateDayStrings(#days_with_hours) = [ ['Sunday', 'Sun'], ['Saturday', 'Sat'] ]
For your particular codebase, it'd probably look something like this:
module PaymentsHelper
...
def generatePaymentAmountSelectArray (unavailable_denoms)
all_denoms = [ ['$ 0', '0'],
['$ 1', '1'],
['$ 2', '2'],
['$ 5', '5'],
['$ 10', '10'] ]
# or all_denoms = Payment::AMOUNT_VALUES
# although not sure how this works with a hash.
all_denoms.reject do |current_denom|
(current_denom - unavailable_denoms).size != 2
end
end
...
end
<%= f.select :amount, generatePaymentAmountSelectArray(#used_values), {:blank => false}) %>
# just make sure you set #used_values in your controller
I feel like there's a better way of doing this, so I'll update if I remember it.

Strange behaviour if amount is set to 0.5

My form has the following:
<%= f.select(:amount, Order::AMOUNT_VALUES, {:blank => false}) %>
My model has:
AMOUNT_VALUES = { '$ 0.50' => 0.5, '$ 1' => 1, '$ 2' => 2, '$ 5' => 5, '$ 10' => 10 }
validate :amount_values_to_be_of_certain_values
def amount_values_to_be_of_certain_values
puts self.amount
unless AMOUNT_VALUES.has_value? self.amount
errors.add(:amount, 'not a valid field')
end
end
If I select 0.5 and submit the form. The form complains saying "Amount not a valid field". If I select one of the other values, the form submits fine.
It seems the problem is with a decimal point that starts with 0.
What is wrong here?
If you're storing amount in an integer field, then it's going to round any floating point numbers you put into it down. That's why self.amount.to_f is 0.0, because putting 0.5 into an integer field changes it to 0, and 0.to_f is 0.0.

Change date_select values from english to spanish in rails

I am building a site in rails and I have a date selector with a drop down menu that Rails generate automatically. The problem is that my site is in spanish and the values of the dropdown for the months are in English, is there a way to change the language to spanish?
I tried adding some lines of codes to the config/environment.rb that I found here
The code is basically this:
require 'date'
class Date
MONTHNAMES = [nil] + %w(Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre Octubre Noviembre Diciembre)
module Format
MONTHS = {
'Enero' => 1, 'Febrero' => 2, 'Marzo' => 3, 'Abril' => 4,
'Mayo' => 5, 'Junio' => 6, 'Julio' => 7, 'Agosto' => 8,
'Septiembre'=> 9, 'Octubre' =>10, 'Noviembre' =>11, 'Diciembre'=>12
}
end
end
But nothing changed after I fired up the server again.
I hope you can help me, thanks in advance.
From the documentation:
:use_month_names - Set to an array with 12 month names if you want to customize month names. Note: You can also use Rails’ i18n functionality for this.
So you can either do this:
<%= f.date_select :date, {:use_month_names => ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre','Diciembre']} %>
Or, for bones internationalisation points, replace the strings with t() method calls and use Rails's I18n localisation files:
<%= f.date_select :date, {:use_month_names => [t(:jan), t(:feb), t(:mar), t(:apr), t(:may), t(:jun), t(:jul), t(:aug), t(:sep), t(:oct), t(:nov), t(:dec)]} %>
In config/locales/es.yml
es:
jan: "Enero"
feb: "Febrero"
...
And then in config/application.rb set:
config.i18n.default_locale = :es
Bingo! :-)
In Rails 4:
(polish case:)
= f.datetime_select :start_time, prompt: {day: 'Dzień', month: 'Miesiąc', year: 'Rok'}
pl:
date:
order: ["year", "month", "day"]
month_names: ["Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"]
it's all
in rails 5 > easiest and best scalable way
in your view:
<%= f.date_select :start_time %>
in your config/locales/en.yml add this :
en:
date:
order: ["day", "year", "month"]
month_names: ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "August", "September", "Oktober", "November", "December"]
Just change the month names in between the strings in whatever you want.
For rails 4.2.5.1 or higher you can use t('locale_parameter_name') instead t(:locale_parameter_name)
E.g:
:use_month_names => [t('jan'), t('feb'), t('mar'), t('apr'), t('may'), t('jun'), t('jul'), t('aug'), t('sep'), t('oct'), t('nov'), t('dec')]

What statistical technique would be appropriate for optimising the weights?

Background:
For example I have the following data
headings = {
:heading1 => { :weight => 25, :views => 0, :conversions => 0}
:heading2 => { :weight => 25, :views => 0, :conversions => 0}
:heading3 => { :weight => 25, :views => 0, :conversions => 0}
:heading4 => { :weight => 25, :views => 0, :conversions => 0}
}
total_views = 0
I got to serve these headings based on their weightages. Every time a heading is served its views is incremented by one and total_views also incremented. And whenever a user clicks on a served heading its conversions is incremented by one. I've written a program (in Ruby) which is performing this well.
Question:
I need to Auto Optimize best converting heading. Consider the following views and conversions for all headings:
heading1: views => 50, conversions => 30
heading2: views => 50, conversions => 10
heading3: views => 50, conversions => 15
heading4: views => 50, conversions => 5
I need to automatically increase the weightage of heading(s) which is/are converting more and vice versa. The sum of weightage will always be 100.
Is there any standard algorithm/formula/technique to do this? There might be some other parameters that need to predefined before making these calculations. But I am not getting it through.
Pleas advise.
Thanks,
Imran
Well, a simple percentage calculation would do that.
30+10+15+5=60
30/60*100=50
10/60*100=17 (rounded)
15/60*100=25
5/60*100=8 (rounded)
So you get weightings of 50,17,25 and 8. You may or may not nead to watch out for rounding errors.