简体   繁体   English

如何使页面不在Rails中重定向

[英]How to make the page not redirect in Rails

I have an erb file named index that has a form in it. 我有一个名为index的erb文件,其中包含一个表单。 As a simple example: 作为一个简单的例子:

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

When I click the BAR! 当我单击BAR时! button it preforms the actions I expect and it forwards me onto the bar.erb file, displaying the expected output. 按钮,它执行我期望的操作,并将我转发到bar.erb文件,显示期望的输出。 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. 但是,我想做的是从此页面获取生成的html,并将其填充到索引页面上div的innerHTML中。 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: 您应该能够像这样传递div的ID进行更新:

<% 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. Rails将寻找一个名为bar.js的模板,并将其呈现并将其内容返回到浏览器,而无需重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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