I'm having an issue with a Chartkick Timeline chart in Rails 4.
When the page loads this error is thrown:
"undefined method `each_pair' for#< Array:... >"
in the Chartkick code as shown below;
def chartkick_deep_merge(hash_a, hash_b)
hash_a = hash_a.dup
hash_b.each_pair do |k, v|
tv = hash_a[k]
hash_a[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
end
hash_a
end
I have attempted various iterations of the code using to_json, as_json, #events.each do ..., #events.map... attempted
timeline("chart-1", "phase_masters_phase_time_path")
that points to
render json: Events.select(:id, :event_types, :starttime, :endtime).where(project_masters_id: params[:proj_id], phase_masters_id: params[:id])
The variables appear to be being set correctly as can be seen in hash_b
hash_a
{:width=>"400px", :height=>"400px", :colors=>["#B9D3EE", "#9FB6CD", "#A2CD5A", "#6E8B3D", "#FFB90F", "#E3A869"]}
hash_b
[[#<Events id: 1, event_types: "Estimating", starttime: "2015-10-05 11:15:00", endtime: "2015-10-13 21:15:00">], [#<Events id: 2, event_types: "Roll Forming", starttime: "2015-09-09 11:00:01", endtime: "2015-09-16 18:29:59">], [#<Events id: 7, event_types: "Magest On Site", starttime: "2015-09-25 11:00:01", endtime: "2015-09-25 18:29:59">]]
view:
<div id = "chart-1"> </div>
<%=
timeline("chart-1", #events.map { |evnt| [evnt] } )
%>
controller:
#events = Events.select(:id, :event_types, :starttime, :endtime).where(project_masters_id: params[:proj_id], phase_masters_id: params[:id])
respond_to do |format|
format.html
format.json {render json: #events}
end
Any help with getting this running would be appreciated.
It seems like your passing an array of Events to the method timeline, that method expects an array of triplets (of type array).
I'm not sure I'm reading your code right but it seems like the call to timeline could instead be something like
timeline("chart-1", #events.map { |evnt| [evnt.types, evnt.starttime, evnt.endtime] })
Related
I would like to know if it is possible to loop through a list of values in SimpleTemplateEngine groovy. For example:
def values = [ "1", "2", "3" ]
def engine = new groovy.text.SimpleTemplateEngine()
def text = '''\
???
'''
def template = engine.createTemplate(text).make(values)
println template.toString()
How can I get:
1
2
3
by changing the variable text?
def values = [ "1", "2", "3" ]
def engine = new groovy.text.SimpleTemplateEngine()
def text = '''<% values.each { println it} %>'''
println engine.createTemplate(text).make([values: values])
Did you mean?
def values = [ "1", "2", "3" ]
def engine = new groovy.text.SimpleTemplateEngine()
def text = '''
${values.each { println it} }
'''
println engine.createTemplate(text).make([values: values])
if you want an elegant template without many quotes and without a lot of imperative programming, you can do the following
def text = '''
<% for (item in values) { %>
<%= item %>
<% } %>
'''
The rule is simple:
Use <%= ..%> if there is rendering of value.
Use <% .. %> if there is flow control handling ( if/else, for loop,... )
#app/serializers/admin_serializer.rb
class AdminSerializer < ActiveModel::Serializer
attributes :id, :email, :access_locked?
end
#app/controllers/dashboard/admins_controller.rb
def index
#search = Admin.search(params[:q])
if params[:page]
#admins = #search.result(:distinct => true).page(params[:page][:number]).per(params[:page][:size])
else
#admins = #search.result(:distinct => true).page(1).per(10)
end
respond_to do |format|
format.html
format.json {render json: #admins}
end
end
On accessing http://dashboard.localhost.com:3000/admins.json I am getting correct links, with page[size] param as 10. The subsequent page has only one admin object to show
"links": {
"self": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"next": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=2&page%5Bsize%5D=10",
"last": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=5&page%5Bsize%5D=10"
}
But if I access the last link here i.e. http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=5&page%5Bsize%5D=10
The subsequent JSON cotains the following incorrect links, with page[size] param as 1. Since this page has only one admin object to show, all the other links thus generated contains page[size]=1
"links": {
"self": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=5&page%5Bsize%5D=10",
"first": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"prev": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=4&page%5Bsize%5D=10"
}
Fixed this by replacing the
#admins = #search.result(:distinct => true).page(params[:page][:number]).per(params[:page][:size]) by #admins = #search.result(:distinct => true).page(params[:page][:number]).per(10)
I am new to Ruby-on-Rails 4. I have created a custom validator but cannot assign value to an array. It shows error
undefined method <<' for nil:NilClass.
It highlights #msg << 1
For instance, my model is like
class User < ActiveRecord::Base
has_secure_password
validates :email,:email_format => true, :on => :create
validates :password, password_format:{with: "upercase"}
end
My custom validator
class PasswordFormatValidator < ActiveModel::EachValidator
#def initilize(options)-Fixed
def initialize(options)
#msg=[]
#password1 = options[:attributes=>[:password]]
#val=options.inspect
super
end
def validate_each(record, attribute, value)
record.errors[attribute] << #val
unless (value.nil? || value.empty?)
#msg << 1
#record.errors[attribute] << "testing"
end
end
end
#val output
{:attributes=>[:password], :complexity=>3, :length=>6, :class=>User(id: integer, email: string, password_digest: string, created_at: datetime, updated_at: datetime)}
You have a typo in your constructor name, it should be initialize and not initilize. This is why your #msg variable is undefined - your constructor has never been called!
Have a nice day.
I'd like to update a database record using where method.
This how I proceed:
def AjoutAuPanier
#book = Book.find(params[:id])
if #book.nbr_exemplaires > 0
#p = Panier.where(user_id: current_user, book_id: #book.id)
if #p.empty? == false
#p.update(#p.id, quantity: #p.quantity + 1)
else
#p = Panier.new(user_id: current_user.id , book_id: #book.id , price: #book.price, quantity: 1)
end
if #p.save
#book.update(nbr_exemplaires: #book.nbr_exemplaires-1)
redirect_to detail_path
else
redirect_to books_list_path
end
else
flash[:notice]='Ce livre n\'est plus disponible'
redirect_to books_list_path
end
end
This, however, gives the following error:
undefined method `id' for #
#p=Panier.where(user_id: current_user, book_id: #book.id)
You must add .first because .where always return an array
#p=Panier.where(user_id: current_user, book_id: #book.id).first
I'm stuck on this:
I need to populate data into my app.
I'm using Promotion for the very first time....
Without ProMotion I use to fetch the data in the init method
Now my code looks like below:
class Parties < ProMotion::TableScreen
attr_accessor :_cells
#news = []
include MyUiModules
title 'Yazarlar'
refreshable callback: :on_refresh,
pull_message: "Pull to refresh",
refreshing: "Refreshing data…",
updated_format: "Last updated at %s",
updated_time_format: "%l:%M %p"
def on_refresh
#MyItems.pull_from_server do |items|
##my_items = items
end_refreshing
#update_table_data
# end
end
def table_data
_cells = []
[{
title: nil,
cells: create_cells(_cells)
}]
end
def will_appear
Barbutton.create_bar(self)
set_attributes self.view, {
backgroundColor: hex_color("DBDBDB")
}
end
def go_to_next
App.delegate.slide_menu.show_menu
end
def create_cells(_cells)
BW::HTTP.get(URL) do |response|
json = BW::JSON.parse response.body.to_str
for line in json
_cells << { title: line["val_for_title"]}
end
end
_cells
end
end
Unfotunately this does return an empty array, and I can't figure out how to solve it.
Thx for your help
You can't do that because BW::HTTP.get is asynchronous !
Instead try something like this:
def on_init
#data = []
end
def table_data
[
{
title: nil,
cells: #data
}
]
end
def on_refresh
BW::HTTP.get(URL) do |response|
#data = []
json = BW::JSON.parse(response.body.to_str)
json.each do |hash|
#data << { title: hash["val_for_title"]}
end
update_table_data
end_refreshing
end
end
Hope it helps :-)