The following code:
require 'csv'
desc "Import Voters from CSV File"
task :import => [:environment] do
file ="db/GOTV.csv"
CSV.foreach(file, :headers => true) do |row|
Voter.create({
:last_name => row[0],
:first_name => row[1],
:middle_name => row[2],
:name_suffix => row[3],
:primary_address => row[4],
:primary_city => row[5],
:primary_state => row[6],
:primary_zip => row[7],
:primary_zip4 => row[8],
:primary_unit => row[9],
:primary_unit_number => row[10],
:phone_number => row[11],
:phone_code => row[12],
:gender => row[13],
:party_code => row[14],
:voter_score => row[15],
:congressional_district => row[16],
:house_district => row[17],
:senate_district => row[18],
:county_name => row[19],
:voter_key => row[20],
:household_id => row[21],
:client_id => row[22],
:state_voter_id => row[23]
})
end
...is throwing the following error:
/Users/ecumbee/Desktop/cloudvoters/lib/tasks/import.rake:35: syntax error, unexpected $end, expecting kEND
end
^
I've tried removing the end, which throws the same error I've tried adding another end but it results in a can not compile error.
Edit:
error when adding a second end statement
Don't know how to build task 'db:import'
In the error message, $end refers to the end of the input file, while kEND refers to the end keyword, so it's complaining about a missing end, not an extra one.
If you still get a syntax error after adding another end, that's something unrelated to this error.
The end in your code is for the CSV.foreach ... do block. You're missing another end for the task ... to block.
If that still gives you a syntax error, edit your question and post that error instead.
I know you said you tried to add another end and it didn't help, but the problem with your file is that it's missing the end keyword that will end the task
task :import => [:environment] do
Then can you give more information about the error you're getting once you add the missing end ?
Related
I am trying to send a post request to a separate app where I need to send a matching signature that is not json encoded and the data, which is json encoded. I keep getting errors when I try to do this, so I'm just not understanding how to format it, and I can't find any examples on SO or their documentation (or anywhere else on the web).
signature = create_signature
result = HTTParty.post("weburl.com/file/to/post_to.php",
:body => {[ signature,
:data => { :timestamp => #message.created_at,
:url => company.request_host,
:name => #message.name,
:email => #message.email,
:phone => #message.phone,
:product => #message.service,
:comments => #message.message
}.to_json
]},
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
:verify => false )
The error I'm getting currently is syntax error, unexpected '}', expecting => ]}, ^
I've also tried without the arrays, without the data array, etc.
How do I format this to properly submit this info?
I think you need to move your .to_json to the end of the object.
:body => {[ signature,
:data => { :timestamp => #message.created_at,
:url => company.request_host,
:name => #message.name,
:email => #message.email,
:phone => #message.phone,
:product => #message.service,
:comments => #message.message
}
].to_json},
I've tested my controller and have got strange errors like this:
expected: ("376")
got: (376)
Please stub a default value first if message might be received with other args as well.
This is my spec:
it 'should send confirm-email if information is good' do
sign_in user
allow(Order).to receive(:find).with(order.id.to_s).and_return(order)
allow(order).to receive(:finalize) {order}
allow(order.errors).to receive(:empty?) {true}
expect(OrderMailer).to receive_message_chain(:send_finish_notification, :deliver)
patch :save_order, {:id => order.id , :order => {:street_address => 'Baker street', :apt => '123#', :zip_id => zip.id, :frequency_id => frequency.id, :amount_per_hour => '5',
:extras_ids => '', :phone_number => '3213', :credit_card_number => '4242424242424242', :credit_card_cvv => '777',
:credit_card_expiration => '12/20', :source_information => ''}}
end
And I've got this error in some logically close specs. But some tests passes, like this one:
it 'should not update user data if order errors is not empty' do
sign_in user
allow(Order).to receive(:find).with(order.id.to_s).and_return(order)
allow(order).to receive(:finalize) {order}
allow(order.errors).to receive(:empty?) {false}
expect(User).to_not receive(:update_user_data)
patch :save_order, {:id => order.id, :order => {:street_address => 'Baker street', :apt => '123#', :zip_id => zip.id, :frequency_id => frequency.id, :amount_per_hour => '5',
:extras_ids => '', :phone_number => '3213', :credit_card_number => '4242424242424242', :credit_card_cvv => '777',
:credit_card_expiration => '12/20', :source_information => ''}}
end
to_s or to_i doesn't help. The error line in controller -
#order = Order.find(params[:id]
So what could be in that case ? 'Cause it looks like some specs passes, but similar to them don't. Any suggestions ?
I'm getting no route matches with rspec for testing a method in my controller.
Below is the test code:
let(:csv_file){ fixture_file_upload('files/sample_employee_data.csv', 'text/csv') }
describe "#process_csv" do
it "should output a valid csv file" do
post '/payslips/process_csv', :csv => csv_file, :header => 1
puts response
end
end
Below is my routes.rb file code:
PayCalculator::Application.routes.draw do
resources :payslips do
collection { post :process_csv }
end
root 'payslips#index'
end
Below is the method
def process_csv(uploaded_file = params[:files][:csv], headers = params[:files][:headers])
begin
rows = CSV_Manager.extract_csv(uploaded_file, headers)
rows.each do |row|
payslip = Payslip.create(
:first_name => row[0],
:last_name => row[1],
:annual_salary => row[2],
:superannuation => row[3].to_i,
:payment_start_date => row[4]
)
redirect_to root_url, notice: payslip.errors.full_messages and return unless payslip.valid?
prepare_output(row)
end
#rows = self.pay_data
csv_file = CSV_Manager.prepare_csv(#rows, ["Name", "Pay Period", "Gross Income", "Income Tax", "Net Income", "Superannuation"])
send_data csv_file, :type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment;filename=Payslip #{Date.today.to_s}.csv"
rescue
redirect_to root_url, notice: "CSV not supplied or invalid format"
end
end
When I run rspec spec/ I get below error:
Failure/Error: post '/payslips/process_csv', :csv => csv_file, :header => 1
ActionController::UrlGeneratorError:
No route matches...
What could be wrong in here that is causing this error?
params[:files][:headers] where you are passing :header => 1. Key is different. This will not cause no route found probably but just for correction. As per rails convention action doesn't has parameters
If you are going to pass optional params in any methods: Please have a look at : http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
Following is the example of method defination:
def foo(arg1="Miles", arg2="Coltrane", arg3="Roach")
"#{arg1}, #{arg2}, #{arg3}."
end
Try this:
post :process_csv, :files => {:csv => csv_file, :header => 1}
I'm trying to add rows of a CSV to records in a table and everything seems to be working fine besides the fact that nothing actually shows up.
Here is my Rake task:
namespace :csv do
desc "Import CSV Data"
task :c_up => :environment do
require 'csv'
csv_file_path = 'db/main.csv'
CSV.foreach(csv_file_path, :headers => true) do |row|
row = Entry.create!({
:one => row[0],
:two => row[1],
:three => row[2],
:four => row[3],
:five =>row[4],
:six => row[5],
:seven => row[6],
:eight => row[7],
:nine => row[8],
:ten => row[9]
})
row.save
puts "Entry added!"
end
end
end
The headers in my CSV are named to match the attributes.
When I run the task I receive no errors and all of the "Entry added!" show up in the console. I've tried it without the row = and row.save as I've seen lots of examples without those but it doesn't help.
After the rake finishes there aren't any records at all. Is there something obvious I'm doing wrong? It seems like this is following most examples I've seen.
Edit Thanks for all your answers and comments! Obviously this is flawed all the way down to syntax, I'm going to try out your suggestions and I'll update this after. Thanks again!
Edit 2 It turned out to be a syntax issue that you guys all pointed out, so +1 for everyone thanks! The reason when I commented it still wasn't working was an unrelated problem which I wouldn't have figured out without #jvnill's comments.
try with:
row = Entry.new(
:one => row[0],
:two => row[1],
:three => row[2],
:four => row[3],
:five =>row[4],
:six => row[5],
:seven => row[6],
:eight => row[7],
:nine => row[8],
:ten => row[9]
)
row.save!
Without { } , and with save!
You could even try with this, to make it simpler:
Entry.create!(row.to_hash)
Entry.create! does not require being assigned to row nor does it require a row.save. Also know '{}' Just do:
Entry.create!(
:one => row[0],
:two => row[1],
:three => row[2],
:four => row[3],
:five =>row[4],
:six => row[5],
:seven => row[6],
:eight => row[7],
:nine => row[8],
:ten => row[9]
)
The create method will populate the row and save it as well.
namespace :csv do
desc "Import CSV Data"
task :c_up => :environment do
require 'csv'
csv_file_path = 'db/main.csv'
CSV.foreach(csv_file_path, :headers => true) do |row|
Entry.create!({
:one => row[0],
:two => row[1],
:three => row[2],
:four => row[3],
:five =>row[4],
:six => row[5],
:seven => row[6],
:eight => row[7],
:nine => row[8],
:ten => row[9]
})
end
end
end
I'm stuck with a rake task that need to prepare a newsletter for Mailchimp.
Using rails 2.x stuff googled I now have this code:
desc "Sends newsletter to Mailchimp list"
task :send_newsletter => :environment do
begin
# get render helpers
av = ActionView::Base.new(Rails::Application::Configuration.new(Rails.root).view_path)
av.class_eval do
include ApplicationHelper
end
things = Stuff.do.things
h = Hominid::Base.new({:api_key => "xxx"})
h.create_campaign(
{
:list_id => "xxx",
:subject => "Hey...",
:from_email => "xxx",
:from_name => "xxx",
:to_email => "",
:auto_footer => true,
:generate_text => true
},
{
:html => av.render(:template => "stuff/newsletter", :locals => {:things => things}, :layout => false)
},
"regular")
rescue Exception => e
STDERR.puts ">>> #{e.to_yaml}"
end
And I get this error message: "undefined method `virtual_path' for false:FalseClass"
My first try was with render_to_string but I just can't access as it is in the controller not the view.
Any help would be greatly appreciated :)
:layout => nil ?