I am working on a Ruby on Rails app that someone else wrote, and the client now wants changed it with a specific flow of picture creation. The flow goes like this:
User goes to www.website.com/pictures/new and sees a form with only a URL field and a submit button, looking like this:
<%= form_for @picture do |f| %>
<%= tag :input, id: 'url-field', size: 30, title: "Submit the URL of your picture" %>
<%= f.hidden_field :url %>
<%= f.submit 'Submit', id: 'url-submit', class: 'blue-btn' %>
<% end %>
When the user submits a url, the form is intercepted by Jquery instead of posting as it normally would. Currently it is posting to a validate action on the controller, like so:
MYAPP.picturesController.newAction = {
init: function() {
var initVars = {
$newPictureForm: $('#new_picture')
$urlSubmitBtn: $('#url-submit')
};
initVars.$newPictureForm.submit(function() {
initVars.$urlSubmitBtn.attr('disabled', 'disabled');
$.post(MYAPP.picturesController.newAction.validationPath, initVars.$newPictureForm.serialize(), function(response) {
initVars.$urlSubmitBtn.removeAttr('disabled');
if (response.status == 'ok') {
initVars.$flashMsg.empty();
$.fn.colorbox({
inline: true,
scrolling: false,
href: '#viewer',
onCleanup: function() {
if ($.browser.msie) {
$('#site-player').removeAttr('src');
}
},
onOpen: function() {
$('#site-player').attr('src', response.picture.embed_url);
},
onLoad: function() {
$('#colorbox').removeClass('cboxDialog');
}
});
} else {
initVars.$flashMsg.text(response.error);
}
}, 'json');
return false;
});
After it has verified the url, it comes back and displays two links: Log in with Twitter and Log in with Facebook. I am using Oauth to complete this. A user then clicks one of those and goes through the verification process. Here is my problem: when the user clicks one of those links, I lose the URL the user submitted. I would usually change up the flow and make the user log in first, but this is exactly how the client wants it.
I know with Oauth you can pass variables to Facebook/Twitter and use them upon returning to your app, like so
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook, url: @picture.url) %> <br />
<%= link_to "Sign in with Twitter", user_omniauth_authorize_path(:twitter, url: @picture.url) %>
but at this point, @picture doesn't contain the URL because it hasn't been posted. And as far as I can tell the POST request jquery makes doesn't set it, here is the validate action:
def validate
@picture = Picture.new(params[:picture])
respond_to do |format|
format.json do
if @picture.valid? # for this post just assume this will return true
render json: { status: :ok, picture: { embed_url: @picture.embed_url }
else
render json: { status: :error, error: @picture.all_errors.first }
end
end
end
end
So my problem is that I need to submit the URL to facebook or twitter so I can create the picture once they've come back, but I can't figure out how to get the URL since it is never posted except as an AJAX post.
I've thought about setting @picture to a global variable $picture in the validate action so I can access $picture in the view, but that seems like it isn't the right solution and could be buggy.
So does anyone have any advice on how I can extract the URL to send with the facebook or twitter request?
Let me know if you need to see any more code.. thanks a ton in advance.
session
hash. In your controller action: if the user is logged in, everything is fine. Otherwise, put the data you want to hold onto in the session (session[:whatever] = params[:whatever]
), redirect them to the OAuth provider, and in the callback action read whatever out of the session hash and do whatever you want to with it. – Amit Kumar Gupta Apr 19 at 5:41