I am currently trying to build a table to display a list of users with their attributes in separate columns.
it is something like:
user1 | user1 email | user1 admin role | user1 organiser role
user2 | user2 email | user2 admin role | user2 organiser role
user3 | user3 email | user3 admin role | user3 organiser role
However, the admin and organiser attributes are stored in a hstore column called activities.
When I build the datatable the browser returns this error
DataTables warning: table id=DataTables_Table_0 - Requested unknown
parameter 'activities.organiser' for row 0. For more information about
this error, please see http://datatables.net/tn/4
This is my code where I build my table columns
in manage_user_controller.rb
private
def load_columns
columns = []
columns << {:data => "id", :title => "ID"}
columns << {:data => "email", :title => "Email"}
columns << {:data => "created_at", :title => "Created"}
columns << {:data => 'activities.organiser', :title => "Organiser Role"}
columns << {:data => 'activities.admin', :title => "Admin Role"}
columns << {:data => "id", :title => ""}
return columns
end
in my users_datatable.rb
private
def data
results = []
users.each_with_index do |user, index|
cols = {"id" => "#{user.id}"}
cols.merge!("email" => "#{user.email}")
cols.merge!("created_at" => "#{user.created_at}")
cols.merge!("activities" => "#{user.activities.organiser}")
cols.merge!("activities" => "#{user.activities.admin}")
#cols.merge!("activities" => "#{user.activities.admin_events}")
results << cols
end
return results
end
I have tried to change "activities.organiser" to "activities" in the controller, it will load some values, but I cant possible use activites for all the sub attributes in activites itself because that will be wrong.
So, how should I correctly populate a datatable with key-value pairs from hstore column?
Thank you very much.
ok, i manage to solve it.
in the manage_users_controller.rb
def load_columns
columns = []
columns << {:data => "id", :title => "ID"}
columns << {:data => "email", :title => "Email"}
columns << {:data => "created_at", :title => "Created"}
columns << {:data => "organiser", :title => "Organiser Role"}
columns << {:data => "admin", :title => "Admin Role"}
columns << {:data => "id", :title => ""}
return columns
end
In the user_datatable.rb
def data
results = []
users.each_with_index do |user, index|
cols = {"id" => "#{user.id}"}
cols.merge!("email" => "#{user.email}")
cols.merge!("created_at" => "#{user.created_at}")
cols.merge!("organiser" => "#{user.activities.organiser}")
cols.merge!("admin" => "#{user.activities.admin}")
#cols.merge!("activities" => "#{user.activities.admin_events}")
results << cols
end
return results
end
the names must be the same in both files.
Related
I have used multi-select for selecting multiple users here. what I have implemented:-
f.fields_for :user_id do r
r.label :email
r.collection_select(:User.all, :id, :email, {:prompt => "select email", multiple: true})
It worked fine and I am able to select multiple users email
But it returns params as
table_attributes => {0 =>
{user_id: ["",1,2,3] } }
Since I am using has_many association so what I want is to get params as nested params like below:-
Table_attributes=>{0 => {user_id: 1}, 1 => {user_id: 2}, 2 => {user_id: 3}}
Can anyone explain how to achieve it.
Thanks in advance
So, I am developing an Ruby on Rails website to import an Excel file into our website. I followed RailsCasts and using Roo gem. All are working, but when the import process done, all fields are nil. When I checked with raise, I found out that the data is nil.
def self.import(file)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
product = find_by_id(row["id"]) || new
product.attributes = row.to_hash.slice(*accessible_attributes)
product.save!
end
end
When I raised row, I got whole data from Excel. But when I raised product, I got nil. How I can put the data correctly so I can get all value correctly? Thank you.
So, after did some practices, I found out that on first test, it was a hash, so what I need to make it work is actually simple but I am not sure if this is the best practice. If someone have a better practice, it will be a good thing, but this is my solution for now.
object = Lead.new(
:first_name => row['First Name'], :last_name => row['Last Name'],
:address => row['Address'], :city => row['City'],
:state => row['State'], :county => row['County'],
:zip_code => row['Zip Code'], :bedrooms => row['Bedrooms'], :bathrooms => row['Bathrooms'],
:last_sale_date => row['Last Sale Date'], :amount_sold => row['Amount Sold'],
:tax_value => row['Tax Value'], :tax_name => row['Tax Name'], :tax_address => row['Tax Address'],
:tax_city => row['Tax City'], :tax_state => row['Tax State'], :tax_postal_code => row['Tax Postal Code'],
:listing_status => row['Listing Status'], :property_type => row['Property Type'],
:square_footage => row['Square Footage'], :days_on_market => row['Days on Market'], :list_date => row['List Date'],
:status_change_date => row['Status Change Date'], :year_built => row['Year Built'],
:mls_id => row['MLS ID'], :last_call_result => row['Last Call Result'], :last_dial_date => row['Last Dial Date'],
:last_contacted => row['Last Contacted'], :last_dial_time => row['Last Dial Time'], :create_date => row['Create Date'],
:edit_date => row['Edit Date'], :source => row['Source'], :list => row['List'],
:call_attempts => row['Call Attempts'], :family_member => row['Family Member'], :notes => row['Notes'],
:group => row['Group'], :manager => row['Manager']
)
I am trying to charge for Authorize .net Gateway using active merchant gem.
Here is the link for gem
- https://github.com/activemerchant/active_merchant
I am sending email,invoice,description,billing_address, shipping_address as option parameter and transaction is successfully done also but when I see the transaction detail, I found every parameter on authorize .net other than invoice number.
here is my code for option -
options = {:email => "sunil#gmail.com",:invoice => "INV-12345",:description => "Amount 50 for INV-12345",:billing_address => { :name => "Sunil Kumar", :address1 => "9888 America Ave. NW",:city => "Oakland", :state => "AK",
:country => "United States", :zip => "94605",:phone => "1234567890"}}
While charging we are using below code -
gateway.purchase((amount*100), creditcard,options)
Now after successful transaction when I see the transaction detail I found every thing other than Invoice#.
Please suggest if any thing left.
You need to change invoice to order_id. so your code would look like.
options = {:email => "sunil#gmail.com",:order_id => "INV-12345",:description => "Amount 50 for INV-12345",:billing_address => { :name => "Sunil Kumar", :address1 => "9888 America Ave. NW",:city => "Oakland", :state => "AK",
:country => "United States", :zip => "94605",:phone => "1234567890"}}
Here is the code from ActiveMerchant.
def add_invoice(xml, options)
xml.order do
xml.invoiceNumber(truncate(options[:order_id], 20))
xml.description(truncate(options[:description], 255))
end
# Authorize.net API requires lineItems to be placed directly after order tag
if options[:line_items]
xml.lineItems do
options[:line_items].each do |line_item|
xml.lineItem do
line_item.each do |key, value|
xml.send(camel_case_lower(key), value)
end
end
end
end
end
end
https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/gateways/authorize_net.rb
I want to prefill my table with the seeds.rb file in app/db
but there I got a problem during storing the right data.
in the table users I have an column called active withe datatype tinyint. so now I want to store with the seeds.rb int values
User.create([ { :id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => 1}])
but it dosn't store 1. It always stores 0 in database.
also tried this:
User.create([ { :id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => true}])
same problem it stores 0 in db.
Whats going wrong?
The most common thing that causes this kind of problems are validations. In this case I would expect that a user needs an email or a password. Please check if
User.create!(
:id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => true
)
passes the validations and creates an user.
Please note that I use create! that would raise an error if it not able to create an user instead of just returning an unsaved one.
solution that works fine for me:
u = User.new(
:id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => 1
)
u.active = true
u.save
When you have a one-to-many association in Rails 3 and accept nested attributes with delete, is it possible to know by looking at the objects (the associated object) whether it's going to be deleted or not?
For example:
group.attributes = {:member_attributes => {"0" => {:id => 1, :name => "John"},
"1" => {:id => 2, :name => "Dave"},
"2" => {:id => 3, :name => "Gus", "_destroy" => true}}}
Is it possible by looking at group.members to know that the one with id 3 is going to be deleted on save?
There's a method for finding that out, called marked_for_destruction?
group.members.each do |member|
puts "#{member.name} => #{member.marked_for_destruction?}"
end
would generate
John false
Dave false
Gus true