如何在 rails 中使用 omniauth 提交隐藏表单
How to submit hidden form with omniauth in rails
我在我的 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" 这是我在文本区域内编写的内容,当我单击按钮时模式弹出并具有我输入的相同内容:
我试过在这里传递@question,但没有传递任何东西。在控制器中,我试图通过 request.env["omniauth.params"] 访问参数,但它始终为空白。
如何访问或将此内容参数传递给 omniauth 并在控制器中访问它?
这里不需要 表单,因为 content
是 隐藏输入 。您可以将其作为 额外参数 传递给 link_to
,如下所示
= link_to "Sign up with Gmail", user_google_oauth2_omniauth_authorize_path(content: "Your desired value"), class: "zocial gmail"
并在 request.env["omniauth.params"]
中寻找 content
更新:
我能想到的唯一方法是使用 Jquery
的 Javascript 将内容的值附加到单击事件的 url
<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>
我在我的 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" 这是我在文本区域内编写的内容,当我单击按钮时模式弹出并具有我输入的相同内容:
我试过在这里传递@question,但没有传递任何东西。在控制器中,我试图通过 request.env["omniauth.params"] 访问参数,但它始终为空白。
如何访问或将此内容参数传递给 omniauth 并在控制器中访问它?
这里不需要 表单,因为 content
是 隐藏输入 。您可以将其作为 额外参数 传递给 link_to
,如下所示
= link_to "Sign up with Gmail", user_google_oauth2_omniauth_authorize_path(content: "Your desired value"), class: "zocial gmail"
并在 request.env["omniauth.params"]
content
更新:
我能想到的唯一方法是使用 Jquery
的 Javascript 将内容的值附加到单击事件的 url<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>