Search code examples
ruby-on-railsomniauthruby-on-rails-4.2omniauth-facebookomniauth-google-oauth2

How to submit hidden form with omniauth in rails


I have integrated google and facebook omniauth in my rails application. The signup and signin works great. But, I also have a form which should be submitted along with omniauth, so that I can access the form content as parameters.

This is the piece of code:

%section#hero.hero-blk
.container
  .intro.text-center
    %h1 Ask Question Instantly
    %h3.hero-tagline Ask anonymously anytime
  .row
    .col-md-6
      .ask-question
        %h2.text-center Talk to us
        %form{method: 'post', action: new_forward_questions_path}
          =hidden_field_tag :authenticity_token, form_authenticity_token
          %textarea.form-control{:autofocus => "", :cols => "20", :id => "question-content", :name => "content", :placeholder => "Describe your question on relationship, marriage. Please type atleast 50 characters", :rows => "7"}
          %div#chars-left
          %input{type: 'hidden', name: 'source', value: 'home'}
          .text-center
            %button#ask-button-index.glow-button.btn.btn-primary{type: 'submit'}
              %img{:alt => "", :src => image_path("incognito-new.svg")}/
              Ask Anonymously

.modal-body
    #dialog-loading-icon.collapse
      %p
        Please wait, submitting your question
      =image_tag "ripple.gif"
    = simple_form_for @question do |f|
      = f.input :content, as: :hidden, input_html: {id: 'hidden_question_content'}
      - if current_user.nil? or current_user.asker.nil? or current_user.asker.new_record?
        .social-sign-in.visitor-oauth.text-center
          = link_to "Sign up with Gmail", user_google_oauth2_omniauth_authorize_path(@question), class: "zocial gmail"
          = link_to "Sign up with Facebook", user_facebook_omniauth_authorize_path, class: "zocial facebook"

Below is the screenshot of html generated As you can see the modal form gets populated with "Oauth Oauth Oauth" This is the content which I am writing inside the textarea and when I click on the button the modal pops up and has the same content I typed: Html Generated

I have tried passing @question here but nothing gets passed. In controller I am trying to access the params through request.env["omniauth.params"] but it is always blank.

How can I access or pass this content parameter to omniauth and access it in controller?


Solution

  • You don't need a form here since the content is a hidden input. You can pass it as extra params to link_to like below

    = link_to "Sign up with Gmail", user_google_oauth2_omniauth_authorize_path(content: "Your desired value"), class: "zocial gmail"
    

    and look for content in request.env["omniauth.params"]

    Update:

    The only way I can think of by appending the value of content to the url on a click event using Javascript of Jquery

    <script type="text/javascript">
      $(".gmail, .facebook").click(function(){
        var hidden_content = $("#hidden_question_content").val();
        var url = $(this).attr('href');
        window.location.href = "url?content="+hidden_content;
      });
    </script>