开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜