How do I clear values from text_field_tag after browser refresh?

895 views Asked by At

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
1

There are 1 answers

0
Milind On

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');
    });
});