Rails invalid date error - mysql

Im using this gem to setup a calendar in my app. Everything is working fine except the arrows that change the month.. Getting the following error:
ArgumentError in UsersController#show
invalid date
Rails.root: /Users/nelsonkeating/Desktop/ReminDeal
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:13:in `show'
Request
Parameters:
{"month"=>"2012-06",
"id"=>"7"}
users_controller.rb
def show
#user = User.find(params[:id])
#friends = #user.friends.paginate(page: params[:page])
#date = params[:month] ? Date.parse(params[:month]) : Date.today
end
Console:
Started GET "/users/7?month=2012-06" for 127.0.0.1 at 2012-05-29 21:30:04 -0700
Processing by UsersController#show as HTML
Parameters: {"month"=>"2012-06", "id"=>"7"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "7"]]
Completed 500 Internal Server Error in 33ms
ArgumentError (invalid date):
app/controllers/users_controller.rb:13:in `show'

It is cause due to improper format of the date, the date format is either "%Y-%m-%d" or"%d-%m-%Y" since you have just pass the parameter i.e.
{"month"=>"2012-06", "id"=>"7"}
you should send the parameter
{"month"=>"2012-06-27", "id"=>"7"}
you should send the date also
I had the same problem and solve by adding day in the date parameter.
Hope this will help you.

It is Date.parse("2012-06") causing the error.
If the date is not significant, you may try this: Date.parse("#{params[:month]}-01")

Related

Running Stored procedure giving error can't return a result set in the given context

I know this problem has been asked many times. But I tried the solutions and they didn't work for me. I have a web application built on rails 3.2.12 and ruby 1.9.2p180. I have a stored procedure in it which returns me data of a query having 5 inner joins. Multiple rows approximately 600 are returned in present case. On the local the stored procedure runs fine with no issues. But when I tried it on the server it is throwing:
ActiveRecord::StatementInvalid: Mysql2::Error: PROCEDURE test.sp_procedure can't return a result set in the given context: call sp_procedure('2015-02-14 00:00:00 -0500', '2015-03-03 23:59:00 -0500', 5, '13')
I have searched for this issue and found that I need to set CLIENT_MULTI_RESULTS flag when establishing connection to MySQL server. For this I have done monkey patching as said. Here is the file in initializers:
module ActiveRecord
class Base
def self.mysql2_connection(config)
config[:username] = 'deploy' if config[:username].nil?
if Mysql2::Client.const_defined? :FOUND_ROWS
config[:flags] = config[:flags] ? config[:flags] | Mysql2::Client::FOUND_ROWS : Mysql2::Client::FOUND_ROWS
end
client = Mysql2::Client.new(config.symbolize_keys)
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
end
def self.select_sp(sql, name = nil)
connection = ActiveRecord::Base.connection
begin
connection.select_all(sql, name)
rescue NoMethodError
ensure
connection.reconnect! unless connection.active?
end
end
end
end
In my database.yml I have added: flags: <%= 65536 | 131072 %> and also tried with flags: 131072. But it didn't work.
However using the following works:
client = Mysql2::Client.new(:host => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS )
result = client.query( 'CALL sp_procedure('2015-02-14 00:00:00 -0500', '2015-03-03 23:59:00 -0500', 5, '13')')
This worked on the server too. But each time the stored procedure will run it will create a new connection which I don't want.
And also one thing to note while I am doing this on local as soon as I call the stored procedure I have to execute this:
ActiveRecord::Base.connection.reconnect!
If I don't write this it throws an error:
ActiveRecord::StatementInvalid: Mysql2::Error: Commands out of sync; you can't run this command now
So this is also the same thing means it creates a new connection each time. So I am finding for a solution which saves me from doing this.
And if the monkey patching is correct then what am I missing. Please help.

sql error database on main page

any idea how to repair this error? I get this error on the main website page
DATABASE ERROR
************************
SELECT SQL_CACHE cid, title_ AS title
FROM ilance_categories
WHERE cattype = 'product'
AND visible = '1'
AND level <= '1'
ORDER BY sort ASC
MySQL Error : Unknown column 'title_' in 'field list'
Error Number : 1054
Date : Aug 12, 2014, 11:33:15
Script : http://www.redsquaretags.com/main.php
Class : ilance_mysql
Location : main
Referrer : Not referred
IP Address : 196.22.238.162
ILance Version : 4.0.0
Build : 8059
SQL Version : 898
MySQL Version : 5.5.37-cll
Server Load : n/a
************************
END DATABASE ERROR
Thank you, that's the only error I have thank you

Why are my json attributes bouncing off in POST in Sinatra?

I hope someone can share their knowledge with me. I have a small Sinatra app that I want to POST json data to it. The record is being created, thankfully, but none of the attributes make it. I've read several JSON examples using curl but they all yield the same result. So it makes me think it's my model. I can create records using tux fine so perhaps it's not my model.
curl command:
curl -XPOST -H "Content-Type: application/json"
"localhost:4567/date" -d
'{ "post": { "title": "different tile here"}}'
curl output:
=> {"id":13,"title":null,"body":null,"created_at":"2014-04-21T18:13:53Z",
"updated_at":"2014-04-21T18:13:53Z"}
sinatra output:
[2014-04-27T20:03:48.035710 #45360] DEBUG -- : (0.1ms) commit transaction
127.0.0.1 - - [27/Apr/2014 20:03:48] "POST /date HTTP/1.1" 200 - 0.0181
{"post":{"title":"different tile here"}}
D, [2014-04-27T20:09:32.614274 #45360] DEBUG -- : (0.1ms) begin transaction
D, [2014-04-27T20:09:32.615917 #45360] DEBUG -- : SQL (0.4ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2014-04-28 01:09:32 UTC], ["updated_at", 2014-04-28 01:09:32 UTC]]
D, [2014-04-27T20:09:32.617656 #45360] DEBUG -- : (1.5ms) commit transaction
D, [2014-04-27T20:09:32.617852 #45360] DEBUG -- : (0.1ms) begin transaction
D, [2014-04-27T20:09:32.618132 #45360] DEBUG -- : (0.0ms) commit transaction
127.0.0.1 - - [27/Apr/2014 20:09:32] "POST /date HTTP/1.1" 200 - 0.0070
D, [2014-04-27T20:09:57.796909 #45360] DEBUG -- : Post Load (0.3ms) SELECT "posts".* FROM "posts" ORDER BY created_at DESC
127.0.0.1 - - [27/Apr/2014 20:09:57] "GET / HTTP/1.1" 200 2149 0.0128
app.rb
require 'sinatra'
require 'sinatra/activerecord'
require './environments'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require 'json'
class Post < ActiveRecord::Base
end
post "/posts" do
#post = Post.new(params[:post])
if #post.save
redirect "posts/#{#post.id}"
else
erb :"posts/create"
end
end
post '/date', :provides => 'json' do
data = JSON.parse(request.body.read)
json_data = data.to_json
content_type :json
##post = Post.new(params)
#post = Post.create(
title: data[:title]
body: data[:body]
)
if #post.save
#post.to_json
else
halt 500
end
end
get "/posts/create" do
#title = "Create post"
#post = Post.new
erb :"posts/create"
end
get "/posts/:id" do
#post = Post.find(params[:id])
#title = #post.title
erb :"posts/view"
end
get "/" do
#posts = Post.order("created_at DESC")
#title = "Welcome."
erb :"posts/index"
end
Gemfile
source 'https://rubygems.org'
ruby "2.0.0"
gem "sinatra"
gem 'activerecord', '4.0.4'
gem "sinatra-activerecord"
gem 'sinatra-flash'
gem 'sinatra-redirect-with-flash'
gem 'json'
group :development do
gem 'sqlite3'
gem "tux"
end
group :production do
gem 'pg'
end
environments.rb
configure :development do
set :database, 'sqlite3:///dev.db'
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb')
ActiveRecord::Base.establish_connection(
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
host: db.host,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: 'utf9'
)
end
I've left off my layout files since I'm testing this POST via this rudimentary API. I've bounced around the few examples of Sinatra API's on the web and don't know what I'm not seeing. What am I overlooking here? It's almost like Strong Parameters in Rails 4, but if so why does tux allow me to create records? thanx, sam

in Ruby on Rails, updated gem causing mysql database rollback in formerly passing Capybara test

The following spec used to pass:
it "should allow me to register" do
fill_in "First name", with: "John"
fill_in "Last name", with: "Peters"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Password confirmation", with: "foobar"
expect { click_button submit }.to change(User, :count).by(1)
end
With a test.log output of:
Started GET "/users/sign_up" for 127.0.0.1 at 2013-08-16 13:12:45 -0400
Processing by RegistrationsController#new as HTML
Rendered users/shared/_service.html.haml (1.9ms)
Rendered users/registrations/_newfields.haml (149.9ms)
Rendered users/shared/_links.haml (3.9ms)
Rendered users/registrations/new.html.haml within layouts/application (180.3ms)
Rendered shared/_header.haml (10.0ms)
Rendered shared/_footer.haml (12.4ms)
Completed 200 OK in 411ms (Views: 331.7ms | ActiveRecord: 30.0ms)
(120.9ms) SELECT COUNT(*) FROM `users`
Started POST "/users" for 127.0.0.1 at 2013-08-16 13:12:47 -0400
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"first_name"=>"John", "last_name"=>"Peters", "email"=>"user#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.4ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'user#example.com' LIMIT 1
SQL (1.5ms) INSERT INTO `users` (<values>) VALUES (NULL, NULL, NULL, NULL, 'USA', '2013-08-16 17:12:47', NULL, NULL, 'user#example.com', '$2a$10$E4/LZAvbf7HvFobBjFQxjOnHuO8cnBNJzMPQ3MMT9oVnou98DGqty', 'John', 1, '--- []\n', NULL, 'Peters', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, '2013-08-16 17:12:47', NULL)
(30.4ms) COMMIT
(0.1ms) BEGIN
(0.5ms) UPDATE `users` SET `last_sign_in_at` = '2013-08-16 17:12:47', `current_sign_in_at` = '2013-08-16 17:12:47', `last_sign_in_ip` = '127.0.0.1', `current_sign_in_ip` = '127.0.0.1', `sign_in_count` = 1, `updated_at` = '2013-08-16 17:12:47', `interest_areas` = '--- []\n' WHERE `users`.`id` = 1
(0.5ms) COMMIT
Redirected to http://www.example.com/
Completed 302 Found in 210ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2013-08-16 13:12:47 -0400
However, after running bundle update, the test now fails with the error:
Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
count should have been changed by 1, but was changed by 0
And test.log output of:
Started GET "/users/sign_up" for 127.0.0.1 at 2013-08-16 13:09:19 -0400
Processing by RegistrationsController#new as HTML
Rendered users/shared/_service.html.haml (2.0ms)
Rendered users/registrations/_newfields.haml (183.5ms)
Rendered users/shared/_links.haml (5.8ms)
Rendered users/registrations/new.html.haml within layouts/application (220.0ms)
Rendered shared/_header.haml (10.2ms)
Rendered shared/_footer.haml (10.7ms)
Completed 200 OK in 623ms (Views: 522.2ms | ActiveRecord: 57.1ms)
(17.3ms) SELECT COUNT(*) FROM `users`
Started POST "/users" for 127.0.0.1 at 2013-08-16 13:09:20 -0400
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"first_name"=>"John", "last_name"=>"Peters", "email"=>"user#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.2ms) BEGIN
(0.1ms) ROLLBACK
Redirected to http://www.example.com/users/sign_up
Completed 302 Found in 11ms (ActiveRecord: 0.3ms)
Started GET "/users/sign_up" for 127.0.0.1 at 2013-08-16 13:09:20 -0400
The divergence occurs after the BEGIN line in the database queries; the new version does a ROLLBACK immediately after.
Only the gems have changed between the passing case and the failing case. The diff between the two commits (mainly a difference between their Gemfile.lock files) can be found here: http://pastie.org/private/d4a49zoegu0j3faigfjfw
Does anyone know whether updating any of those gems could have caused this error?
Edit - Here is the Registration Controller's #create code:
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
## logic of path
redirect_to new_user_registration_path, alert: resource.errors.full_messages.join('<br />')
end
end
Based solely on general experience and not on specific knowledge of those gem version changes, I can say with some confidence that updating that many gems, including at least two gems with a major version change (i.e. delayed_job*), could have caused this error. :-) I know that's not much help, but if you post the related code (e.g. your RegistrationsController), I bet the community can help you find the issue fairly quickly and perhaps help others in the future.
I simply reverted to my old Gemfile.lock, emptied my gem set, and installed them all again (effectively reverting them to their old versions).

OpenShift domain status failing

So I created an account at open shift, created an app, and installed the command line tool. when I do the command rhc domain status it fails:
Loaded suite /usr/bin/rhc-chk
Started
.E
===============================================================================
Error: test_connectivity(Test1_Connectivity)
ArgumentError: too few arguments
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:204:in `sprintf'
201: message = sprintf(get_message(:errors,name),*(args.shift || ''))
202: solution = get_message(:solutions,name)
203: if solution
=> 204: message << "\n" << sprintf(solution,*(args.shift || ''))
205: end
206: message
207: end
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:204:in `error_for'
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:270:in `test_connectivity'
===============================================================================
F
===============================================================================
Failure:
You need to be able to connect to the server in order to test authentication.
<false> is not true.
test_authentication(Test2_Authentication)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:280:in `test_authentication'
277: # Checking Authentication
278: #
279: def test_authentication
=> 280: assert $connectivity, error_for(:cant_connect)
281:
282: data = {'rhlogin' => $rhlogin}
283: response = fetch_url_json("/broker/userinfo", data)
===============================================================================
..F
===============================================================================
Failure: You must have an account on the server in order to test: whether you have a valid key loaded in your agent.
test_03_remote_ssh_keys(Test3_SSH)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:317:in `require_login'
314: end
315:
316: def require_login(test)
=> 317: flunk(error_for(:no_account,test)) if $user_info.nil?
318: end
319:
320: def require_remote_keys(test)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:321:in `require_remote_keys'
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:376:in `test_03_remote_ssh_keys'
===============================================================================
F
===============================================================================
Failure: You must have an account on the server in order to test: connecting to your applications.
test_04_ssh_connect(Test3_SSH)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:317:in `require_login'
314: end
315:
316: def require_login(test)
=> 317: flunk(error_for(:no_account,test)) if $user_info.nil?
318: end
319:
320: def require_remote_keys(test)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:383:in `test_04_ssh_connect'
===============================================================================
Finished in 2.403595 seconds.
7 tests, 8 assertions, 3 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
42.8571% passed
Not really understanding why it's not able to connect. I was able to use: rhc domain show, with no problems.
Anyone have any suggestions on how to fix this?
It's a bug that should get fixed in the upcoming release. Even though you see this error it shouldn't affect any other behaviour.