简体   繁体   中英

How to make the page not redirect in Rails

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM