How do I clear values from text_field_tag after browser refresh? - ruby-on-rails-3

I'm having an issue with text_field_tag. I want to reset the value of
the text_field when I refresh my browser, seems like a very simple
thing, however I'm having hard time finding the answer.
After I input some values, how do i reset the value to nil or empty??
my code is below
<%= text_field_tag 'access_code', nil,
%>
Any help or tip will be greatly appreciated.
This is my session new page and access_code which I enter in text field is define in config.yml file.
If I don't enter any access code in text field still I get the value in my controller which I enter last time...how can I reset the value?......
Thanks in advance!
logs-
:
Started POST "/login" for 127.0.0.1 at 2015-06-09 16:14:26 +0530
I, [2015-06-09T16:14:26.368484 #6707] INFO -- : Processing by SessionsController#new as HTML
I, [2015-06-09T16:14:26.368731 #6707] INFO -- : Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dw7fO0ZyO4/ZPnNknuXbt4xsLkGWlxM3rmaEYvClTDRjq0dLdVUGt84Pw2ZNBQ8Sz2x7BGZwlHCmq3EozDmoJg==", "access_code"=>"12345", "login"=>"admin", "password"=>"[FILTERED]", "commit"=>"Log in"}
D, [2015-06-09T16:14:26.372837 #6707] DEBUG -- : [1m[36mActiveRecord::SessionStore::Session Load (0.7ms)[0m [1mSELECT `sessions`.* FROM `sessions` WHERE `sessions`.`session_id` = '261e811b132a8cf574d6733a2bf5a2f4' ORDER BY `sessions`.`id` ASC LIMIT 1[0m
I, [2015-06-09T16:14:26.376843 #6707] INFO -- : checking for Mobile Device - Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.81 Safari/537.1
D, [2015-06-09T16:14:39.505665 #6707] DEBUG -- : [1m[35mSQL (1.1ms)[0m SELECT `users`.`id` AS t0_r0, `users`.`login` AS t0_r1, `users`.`email` AS t0_r2, `users`.`encrypted_password` AS t0_r3, `users`.`password_salt` AS t0_r4, `users`.`created_at` AS t0_r5, `users`.`updated_at` AS t0_r6, `users`.`remember_token` AS t0_r7, `users`.`remember_token_expires_at` AS t0_r8, `users`.`reset_password_token` AS t0_r9, `users`.`enabled` AS t0_r10, `users`.`customer_id` AS t0_r11, `users`.`status` AS t0_r12, `users`.`plain_password` AS t0_r13, `users`.`last_checked_at` AS t0_r14, `users`.`sign_in_count` AS t0_r15, `users`.`current_sign_in_at` AS t0_r16, `users`.`last_sign_in_at` AS t0_r17, `users`.`current_sign_in_ip` AS t0_r18, `users`.`last_sign_in_ip` AS t0_r19, `users`.`created_by` AS t0_r20, `users`.`failed_attempts` AS t0_r21, `users`.`disable_reason` AS t0_r22, `customers`.`id` AS t1_r0, `customers`.`name` AS t1_r1, `customers`.`customer_type` AS t1_r2, `customers`.`parent_id` AS t1_r3, `customers`.`created_at` AS t1_r4, `customers`.`updated_at` AS t1_r5, `customers`.`meta_data` AS t1_r6, `customers`.`opt_out` AS t1_r7, `customers`.`fulfilled` AS t1_r8, `customers`.`complete` AS t1_r9, `customers`.`fulfillment_issue` AS t1_r10, `customers`.`deleted_at` AS t1_r11, `customers`.`generic_bulletin` AS t1_r12, `customers`.`archived` AS t1_r13, `customers`.`vendor_rep` AS t1_r14, `customers`.`vendor_rep_email` AS t1_r15, `customers`.`vendor_rep_phone_no` AS t1_r16, `customers`.`converted_to_new_year` AS t1_r17, `customers`.`fulfillment_info` AS t1_r18, `customers`.`slug` AS t1_r19, `customers`.`start_date` AS t1_r20, `customers`.`end_date` AS t1_r21, `customers`.`bcc_on_confirmation` AS t1_r22, `customers`.`sales_tax_type` AS t1_r23, `customers`.`sales_tax_rate` AS t1_r24, `customers`.`delta` AS t1_r25, `customers`.`time_zone` AS t1_r26, `customers`.`commission_perc_req_items` AS t1_r27, `customers`.`commission_perc_optional_items` AS t1_r28, `customers`.`commission_perc_spirit_items` AS t1_r29, `customers`.`commission_perc_blended_rate` AS t1_r30, `customers`.`credit_card_processing_rate` AS t1_r31, `customers`.`monthly_service_fee` AS t1_r32, `customers`.`annual_service_fee` AS t1_r33, `customers`.`store_building_fee` AS t1_r34, `customers`.`account_setup_fee` AS t1_r35, `customers`.`fee_calculated_pre_tax` AS t1_r36, `customers`.`fee_calculated_post_tax` AS t1_r37, `customers`.`handling_fee` AS t1_r38, `customers`.`advertisement` AS t1_r39, `customers`.`payment_receiver_override` AS t1_r40, `customers`.`banner_image` AS t1_r41, `customers`.`default_payment_receiver` AS t1_r42, `customers`.`handling_fee_type` AS t1_r43, `customers`.`support_email_address` AS t1_r44, `customers`.`discount_per_order` AS t1_r45, `customers`.`discount_per_order_min_amount` AS t1_r46, `customers`.`player_number_required` AS t1_r47, `customers`.`discount_text` AS t1_r48, `customers`.`discount_max_counts` AS t1_r49, `customers`.`discount_coupon_code` AS t1_r50, `customers`.`accept_check` AS t1_r51, `customers`.`accept_credit_card` AS t1_r52, `customers`.`store_shipping_rates` AS t1_r53, `customers`.`mobile_version_enabled` AS t1_r54, `customers`.`reminder_email` AS t1_r55, `customers`.`returning_player` AS t1_r56, `customers`.`edit_order_after_closed` AS t1_r57, `customers`.`custom_text_for_receipt` AS t1_r58, `customers`.`eligibility_check_for_free_items` AS t1_r59, `customers`.`eligible_for_free_items` AS t1_r60, `customers`.`storefront_intro` AS t1_r61, `customers`.`pickup_locations` AS t1_r62, `customers`.`enable_facebook` AS t1_r63, `customers`.`allow_customer_notes_storefront` AS t1_r64, `customers`.`accept_cash` AS t1_r65, `customers`.`fb_page_id` AS t1_r66, `customers`.`league_enabled` AS t1_r67, `customers`.`status` AS t1_r68, `customers`.`agree_store_terms` AS t1_r69, `customers`.`launch_store` AS t1_r70, `customers`.`allow_shipping` AS t1_r71, `customers`.`amex_payment_method` AS t1_r72, `customers`.`copied_start_fresh_from` AS t1_r73, `customers`.`custom_message` AS t1_r74 FROM `users` LEFT OUTER JOIN `customers` ON `customers`.`id` = `users`.`customer_id` WHERE `users`.`login` = 'admin' AND `customers`.`deleted_at` IS NULL ORDER BY `users`.`id` ASC LIMIT 1
I, [2015-06-09T16:14:39.511565 #6707] INFO -- : Authentication:: Failed Login for User ID 1 Failed Login Attempts set to 2
D, [2015-06-09T16:14:39.512570 #6707] DEBUG -- : [1m[36m (0.3ms)[0m [1mBEGIN[0m
D, [2015-06-09T16:14:39.527978 #6707] DEBUG -- : [1m[35mUser Exists (0.9ms)[0m SELECT 1 AS one FROM `users` WHERE (`users`.`login` = 'admin' AND `users`.`id` != 1) LIMIT 1
D, [2015-06-09T16:14:39.534245 #6707] DEBUG -- : [1m[36mSQL (0.6ms)[0m [1mUPDATE `users` SET `failed_attempts` = 2, `updated_at` = '2015-06-09 10:44:39.514661' WHERE `users`.`id` = 1[0m
D, [2015-06-09T16:14:39.611739 #6707] DEBUG -- : [1m[35m (73.7ms)[0m COMMIT
I, [2015-06-09T16:14:39.623991 #6707] INFO -- : Rendered sessions/new.html.erb within layouts/application (5.1ms)
I, [2015-06-09T16:14:42.294270 #6707] INFO -- : Completed 200 OK in 15925ms (Views: 2680.0ms | ActiveRecord: 77.4ms)
D, [2015-06-09T16:14:42.295683 #6707] DEBUG -- : [1m[36m (0.3ms)[0m [1mBEGIN[0m
D, [2015-06-09T16:14:42.302890 #6707] DEBUG -- : [1m[35mSQL (0.5ms)[0m UPDATE `sessions` SET `data` = 'BAh7CUkiEmRvbWFpbl92ZW5kb3IGOgZFRkkiB25hBjsAVEkiEF9jc3JmX3Rv\na2VuBjsARkkiMWJLV1ljRE1uUFRnWE1iQUMwK0RVcFVNQVZVWHc1NGRIQ00z\nMVNqeWM1Qkk9BjsARkkiEnBhc3NfYXR0ZW1wdHMGOwBGaQdJIgpmbGFzaAY7\nAFR7B0kiDGRpc2NhcmQGOwBUWwZJIgplcnJvcgY7AEZJIgxmbGFzaGVzBjsA\nVHsGSSIKZXJyb3IGOwBGSUM6HkFjdGl2ZVN1cHBvcnQ6OlNhZmVCdWZmZXIi\nAaFZb3VyIHVzZXJuYW1lIG9yIHBhc3N3b3JkIGlzIGluY29ycmVjdC4gPGJy\nLz5QbGVhc2UgdHJ5IGFnYWluIG9yIGNsaWNrIHRoZSBmb2xsb3dpbmcgbGlu\nayB0byByZXNldCB5b3VyIHBhc3N3b3JkOiA8YSBocmVmPScvZm9yZ290X3Bh\nc3N3b3JkJz5Gb3Jnb3QgUGFzc3dvcmQ8L2E+Lgc7AFQ6D0BodG1sX3NhZmVU\n', `updated_at` = '2015-06-09 10:44:42.298136' WHERE `sessions`.`id` = 45
D, [2015-06-09T16:14:42.363066 #6707] DEBUG -- : [1m[36m (58.4ms)[0m [1mCOMMIT[0m

its its a js request,then you need to explicitly use jquery to make all text fields html as empty as you are using javascript to submit a form and NOT html which would have caused a total page refresh by using redirect_to: new_session_url.So in your login method,you must use something like:-
def login
##save the user info
respond_to do |format|
format.js
end
end
login.js.erb
$("#login-textbox").val(" ");
OR, YOU CAN ADD PAGE SPECIFIC JAVASCRIPT TO CLEAR TEXTBOXES ONSUBMIT
//add this js in your page
$(document).ready(function() {
$('#btnSubmit').click(function(e) {
var isValid = true;
$('input[type="text"]').each(function() {
if ($.trim($(this).val()) == '') {
isValid = false;
$(this).css({
"border": "1px solid red",
"background": "#FFCECE"
});
}
else {
$(this).css({
"border": "",
"background": ""
});
}
});
if (isValid == false)
e.preventDefault();
else
alert('Thank you for submitting');
});
});

Related

psql insert into if else

I want to make it so that when creating a user, the first user receives $ 1000 on the wallet, and the rest - $ 10 (user_id - pk)
I made this request
INSERT INTO users (user_id, login, balance, encrypted_password)
select 1, 'login',
case
when user_id = 1 then (login = 'login', balance= 1000, encrypted_password = 'password')
else (login = 'login', balance = 10, encrypted_password = 'password')
end as user_id
RETURNING user_id
but when I make this request, I get the error:
ERROR: ERROR: column "user_id" does not exist
LINE 4: when #user_id == 0 then (login = 'login', balance= '...
^
HINT: There is a "user_id" column in the "users" table, but it cannot be referenced from this part of the query.
SQL state: 42703
character: 103

LIKE query not working as expected

I have an SQL Query that looks like:
SELECT "medical_interventions"."id" AS t0_r0,
"medical_interventions"."refcode" AS t0_r1,
"medical_interventions"."intervention_tid" AS t0_r2,
"medical_interventions"."medical_intervention_category_id" AS t0_r3,
"medical_interventions"."created_at" AS t0_r4,
"medical_interventions"."updated_at" AS t0_r5, "translations"."id" AS
t1_r0, "translations"."lang" AS t1_r1, "translations"."text" AS t1_r2,
"translations"."created_at" AS t1_r3, "translations"."updated_at" AS
t1_r4 FROM "medical_interventions" LEFT OUTER JOIN "translations" ON
"translations"."id" = "medical_interventions"."intervention_tid" AND
"translations"."lang" = 'fr' WHERE (medical_intervention_category_id
=7 AND text like '%é%')
I have a text in there labeled évaluer
When I search fore just é as in the query above, I get évaluer as I expect. However, if I try év:
SELECT "medical_interventions"."id" AS t0_r0,
"medical_interventions"."refcode" AS t0_r1,
"medical_interventions"."intervention_tid" AS t0_r2,
"medical_interventions"."medical_intervention_category_id" AS t0_r3,
"medical_interventions"."created_at" AS t0_r4,
"medical_interventions"."updated_at" AS t0_r5, "translations"."id" AS
t1_r0, "translations"."lang" AS t1_r1, "translations"."text" AS t1_r2,
"translations"."created_at" AS t1_r3, "translations"."updated_at" AS
t1_r4 FROM "medical_interventions" LEFT OUTER JOIN "translations" ON
"translations"."id" = "medical_interventions"."intervention_tid" AND
"translations"."lang" = 'fr' WHERE (medical_intervention_category_id
=7 AND text like '%év%')
I get absolutely no results.
Is there something I do not understand about LIKE? Is there an alternate function I could use for searches?
AR:
def self.find_by_search_query_and_problem_id(search_query, problem_id)
problem = Problem.find(problem_id)
category = ProblemCategory.find(problem.category_id)
medIntCategory = MedicalInterventionCategory.find_by_category_text(category.category.text)
search = search_query
query = "medical_intervention_category_id =" + medIntCategory.id.to_s + " AND text like ?"
return self.includes(:intervention).where(query, "%#{search}%").references(:intervention)
end
In the controller
def search
#interventions = MedicalIntervention.find_by_search_query_and_problem_id(URI.unescape(params[:search_query]),params[:problem_id])
end
In JS:
getInterventionsFromSearch: function(search_query, problem_id)
{
var interventions = new MedicalIntervetionsCollection();
// fetch data on url AJAX
if(search_query != "")
{
interventions.url = "medical_interventions_search?search_query="+encodeURIComponent(search_query) +
"&problem_id=" + problem_id;
interventions.fetch();
}
return interventions;
}
See this post: How to SQL compare columns when one has accented chars?
Another idea:
There is an easy solution, but not very elegant.
Use the REPLACE function, to remove your accents. Exemple:
SELECT YOUR_COLUMN FROM YOUR_TABLE WHERE replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace( lower(YOUR_COLUMN), 'á','a'), 'ã','a'), 'â','a'), 'é','e'), 'ê','e'), 'í','i'),
'ó','o') ,'õ','o') ,'ô','o'),'ú','u'), 'ç','c') LIKE 'SEARCH_KEY%'
Where SEARCH_KEY is the key word that you wanna find on the column.
Or:
A possible solution would be a User-Defined-Function (UDF). There is a document here describing how to create such a function for SQLite in PHP. You could write a function called DROPACCENTS()

BigQuery Asynchronus Query Job garbles the Query String

i'm using the Ruby Google API Client to call BigQuery. The Query works just fine if I call it synchronously via bq.jobs.query.
Here's the job: job_sQUillv1JOSPg7XdJa7k8JTexPc
Here's the actual query:
select uid,group_concat(event) as path from
(select uid, event,t from
(select properties.distinct_id as uid, event , properties.time as t, t1.ev as ev, t1.time as originalTime from
[ziptrips.ziptrips_events ]
join
( select properties.distinct_id as user, properties.time as time, event as ev from
[ziptrips.ziptrips_events ] where properties.time > 1385856000 and properties.time < 1388534399 and event = 'Home Page Loaded'
group by user,time,ev) as t1
on t1.user = properties.distinct_id
where properties.time < t1.time)
group by uid,event ,t
order by t desc)
group by uid
However when I do the same call asynchronously via bq.jobs.insert, the query string gets garbled (It has a whole bunch of \n and \u003e etc). And the job fails.
select uid,group_concat(event) as path from\n (select uid, event,t from \n (select properties.distinct_id as uid, event , properties.time as t, t1.ev as ev, t1.time as originalTime from \n [ziptrips.ziptrips_events ] \n join \n ( select properties.distinct_id as user, properties.time as time, event as ev from \n [ziptrips.ziptrips_events ] where properties.time \u003e 1385856000 and properties.time \u003c 1388534399 and event = 'Home Page Loaded' \n group by user,time,ev) as t1 \n on t1.user = properties.distinct_id \n where properties.time \u003c t1.time) \n group by uid,event ,t \n order by t desc) \n group by uid
Here's the code:
client,bq = initialize()
config = {
'query' => {
'query' => query
}
}
result = client.execute(:api_method => bq.jobs.insert,
:parameters => {'projectId' => 'onefold-1'},
:body_object => {
"configuration" => config,
"kid" => "bigquery#queryRequest",
"query" => query,
"maxResults" => 1000,
"defaultDataset" => {
"datasetId" => "#{dataset_id}",
"projectId" => 'onefold-1'
},
"timeoMs" => 3000,
"dryRun" => false,
"preserveNulls" => false,
"useQueryCache" => true,
"miCompletionRatio" => 1.0
}
)
I believe it has something to do with encoding etc. Can't put my finger on whether and where to specify the encoding. Appreciate any pointers or suggestions.
Thanks,
Navneet
I found out that this was a non-issue. There was a different error happening at the same time and I linked that error to this.
It works!

Model "update_attributes" returns false, yet model.errors.messages.inspect returns {}

I am looking into a model "update_attributes" returning false, yet model.errors.messages.inspect returns {}. There is no errors, just a transaction rollback. Below is the logs:
===========>>>>>>>>>>> #user.errors.messages.inspect ======
{}
Started PUT "/authentify/users/4" for 127.0.0.1 at 2012-12-20 00:01:57 -0600
Processing by Authentify::UsersController#update as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"Y01zI3oQmccemTJvBnHmff04w4raAcBLwdQ2k69wKjM=", "user"=>{"name"=>"Janexx", "login"=>"amine4", "update_password_checkbox"=>"[FILTERED]", "status"=>"active", "user_levels_attributes"=>{"0"=>{"sys_user_group_id"=>"admin", "manager_id"=>"admin", "id"=>"4"}}}, "commit"=>"Save", "id"=>"4"}
Authentify::User Load (0.0ms) SELECT "authentify_users".* FROM "authentify_users" WHERE "authentify_users"."id" = ? LIMIT 1 [["id", 1]]
Authentify::Session Load (0.0ms) SELECT "authentify_sessions".* FROM "authentify_sessions" LIMIT 1
CACHE (0.0ms) SELECT "authentify_sessions".* FROM "authentify_sessions" LIMIT 1
Authentify::User Load (0.0ms) SELECT "authentify_users".* FROM "authentify_users" WHERE "authentify_users"."id" = ? LIMIT 1 [["id", "4"]]
(0.0ms) begin transaction
Authentify::UserLevel Load (0.0ms) SELECT "authentify_user_levels".* FROM "authentify_user_levels" WHERE "authentify_user_levels"."user_id" = 4 AND "authentify_user_levels"."id" IN (4)
(1.0ms) UPDATE "authentify_users" SET "name" = 'Janexx', "updated_at" = '2012-12-20 06:01:57.100454' WHERE "authentify_users"."id" = 4
(3.0ms) rollback transaction
{}

Doing just one query search, saving it to an array and then search only the array instead of doing multiple sql queries

i have this
class PagesController < ApplicationController
def index
#textos = Texto.all
#texto_historia = Texto.where(:title => "História").first.contents
#texto_capas_para_sofa = Texto.where(:title => "Capas para Sofá").first.contents
#texto_cortinas = Texto.where(:title => "Cortinas").first.contents
#texto_almofadas = Texto.where(:title => "Almofadas").first.contents
end
The SQL output is:
←[1m←[36mTexto Load (2.0ms)←[0m ←[1mSELECT "textos".* FROM "textos"←[0m
←[1m←[35mTexto Load (1.0ms)←[0m SELECT "textos".* FROM "textos" WHERE ("textos"."title" = 'Hist├│ria') LIMIT 1
←[1m←[36mTexto Load (0.0ms)←[0m ←[1mSELECT "textos".* FROM "textos" WHERE ("textos"."title" = 'Capas para Sof├í') LIMIT 1←[0m
←[1m←[35mTexto Load (1.0ms)←[0m SELECT "textos".* FROM "textos" WHERE ("textos"."title" = 'Cortinas') LIMIT 1
←[1m←[36mTexto Load (1.0ms)←[0m ←[1mSELECT "textos".* FROM "textos" WHERE ("textos"."title" = 'Almofadas') LIMIT 1←[0m
←[1m←[35mTexto Load (0.0ms)←[0m SELECT "textos".* FROM "textos" WHERE ("textos"."title" = 'Informa├º├╡es de Contato') LIMIT 1
What i want is to do just one query for all "textos" model and then search inside an array or anything like that to get the specific variable.
You will need to get the array of all ActiveRecord objects and convert it into a hash storing the data you need.
#textos = Texto.all.inject({}) {|h, obj| h[obj.title] = obj.contents; h }
Then you will be able to access your contents with #textos["title"].
I think you want the find or find_all option:
#texto_historia = #texto.find { |a| a.title = "História"}