0

I have an erb file named index that has a form in it. As a simple example:

<% form_for @foo, :url => {:action => 'bar'} do |f| %>
  <%= f.submit "BAR!" %>
<%end%>

When I click the BAR! button it preforms the actions I expect and it forwards me onto the bar.erb file, displaying the expected output. What I would like to be able to do, however, is to take the generated html from this page and stuff it into the innerHTML of a div on the index page. I assume there is a way but I must ask, is there a way to achieve this? Are there any examples available that would be helpful? Thanks!

keybored
  • 5,194
  • 13
  • 45
  • 70
  • seems to be a good work for an ajax call ... easy way is to use a remote_form_for , than in your controller you could use prototype_helper to perform a replace html in the ajax response. you have to google prototypehelper(or jruby if you prefer jquery) remote_form_for , rjs . – andrea Feb 04 '11 at 01:37
  • Just remember that `remote_form_for` isn't in Rails 3. It was replaced by a cleaner solution, see http://stackoverflow.com/questions/3890315/rails-3-undefined-method-remote-form-for – raidfive Feb 04 '11 at 03:22

1 Answers1

0

You should be able to pass the id of the div to update like so:

<% remote_form_for @foo, :url => {:action => 'bar'}, :update => 'id-of-div-to-update' do |f| %>
  <%= f.submit "BAR!" %>
<%end%>

In the controller:

def bar
  # your code here
  respond_to do |format|
      format.html { redirect_to(xxx) }
      format.js   
    end
end

Rails will look for a template named bar.js and will render it and return it's content to the browser without a redirect.

Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40