如何在rails

时间:2017-08-16 12:07:22

标签: ruby-on-rails omniauth ruby-on-rails-4.2 omniauth-facebook omniauth-google-oauth2

我已经在我的rails应用程序中集成了google和facebook omniauth。注册和登录效果很好。但是,我还有一个表格应该与omniauth一起提交,以便我可以访问表单内容作为参数。

这是一段代码:

%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"

下面是html生成的屏幕截图正如你可以看到模态表单填充了“Oauth Oauth Oauth”这是我在textarea中写的内容,当我点击按钮时,模态会弹出并具有我输入的内容相同: Html Generated

我试过在这里传递@question但没有任何传递。在控制器中,我试图通过 request.env [“omniauth.params”] 访问参数,但它总是空白。

如何访问或传递此内容参数到omniauth并在控制器中访问它?

1 个答案:

答案 0 :(得分:0)

您不需要表单,因为content隐藏输入。您可以将 extra params 传递给link_to,如下所示

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

并在content

中查找request.env["omniauth.params"]

<强>更新

我能想到的唯一方法是使用Javascript的Javascript将点击事件的内容值附加到网址

<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>