views:

230

answers:

3

Hi, I'm a jQuery total n00b. In my rails app this what happen:

I'm on the homepage, I click this link:

<a href='/betas/new' rel='facebox'>Sign up</a>

A beautiful facebox popup shows up and render this views and the containing form:

# /app/views/invites/new

<% form_tag({ :controller => 'registration_code', :action => 'create' }, :id => 'codeForm') do %>

    <%= text_field_tag :code %>
    <br />
    <%= submit_tag 'Confirm' %>
<% end %>

I clink on submit and if the code is valid the user is taken on another page in another controller:

def create
  # some stuff
  redirect_to :controller => 'users', :action => 'type'
end

Now I would like to render that page INSIDE the SAME popup contains the form, after the submit button is pressed but I have NO IDEA how to do it. I've tried FaceboxRender but this happens:

Original version:

# /controllers/users_controller
def type
end

If I change it like that nothing happens:

# /controllers/users_controller
def type
  respond_to do |format|
  format.html
  format.js { render_to_facebox }
 end
end

If I change it like that (I know is wrong but I'm a n00b so it's ok :-):

# /controllers/users_controller
def type
  respond_to do |format|
  format.html { render_to_facebox }
  format.js
 end
end

I got this rendered:

try {
jQuery.facebox("my raw HTML from users/type.html.erb substituted here")'); throw e }

Any solutions?

THANK YOU SO MUCH!!

A: 

Hi!

You can continue using facebox_render or one of it's forks, it will make everything a bit easier :)

About your problem, what you need is the form inside the facebox to make an AJAX request so you can use format.js to repond to this call. So, in this case, you need form_remote_tag to post your data using AJAX.

In the controller you will receive an AJAX request, you'll handle it with respond_to.js and then you can make a render_to_facebox again. Now you will see what you want inside of the facebox.

In the view:

# /app/views/invites/new
<% form_remote_tag({ :controller => 'registration_code', :action => 'create' }, :id => 'codeForm') do %>

    <%= text_field_tag :code %>
    <br />
    <%= submit_tag 'Confirm' %>
<% end %>

And in the controller:

# /controllers/users_controller
def type
  respond_to do |format|
    format.html
    format.js { render_to_facebox }
  end
end

I hope it helps a bit, ciao!

Francisco
As I wrote you above now the popup is not loaded, I can't understand why...
Leonardo Dario Perna
Well, I'm afraid I don't know very well what's going on. I would say that there's a problem with jQuery. Did you try to see what's going on in the ajax request? Take a look at firebug and at the server log.
Francisco
firebug console says: "Ajax not defined"...?
Leonardo Dario Perna
This is what the link:<%= facebox_link_to "Link Name", :url => new_beta_url %> is rendered:<a onclick="jQuery.facebox(function(){ new Ajax.Request('http://0.0.0.0:3000/betas/new', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('iMBVH1G0OnU0oMvBWXbwjNAQvoFGWWWqcLrBXSbYtf0=')}) }); return false;" href="#">Link Name</a>the problem is that the popup is not EMPTY. I think the AJAX request is not triggered, but why?
Leonardo Dario Perna
Are you sure that you're properly loading jQuery?
Francisco
A: 

Thank you Francisco, the problem now is in the:

<a href='/betas/new' rel='facebox'>Sign up</a>

I tried to change it in:

<%= facebox_link_to "Sign up", :url => '/betas/new' %>

The facebox popup shows up but don't load the form. What's wrong?

Leonardo Dario Perna
A: 

Now I get an "Ajax not defined" error from the firebug console. What does it means?

Leonardo Dario Perna