简体   繁体   English

如何提交Rails 3表单而不刷新页面?

[英]How do you submit a rails 3 form without refreshing the page?

I've seen this done using ajax & php, but not rails 3. 我已经看到使用ajax和php完成此操作,但未使用rails 3。

I've tried using: 我试过使用:

<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}, :remote => true) do |f| %>  

but the page still refreshes each time. 但是页面仍然每次刷新。 I'm building a point of sale program, so I don't want the page to refresh. 我正在建立一个销售点程序,所以我不想刷新页面。

How do I send the form data to the controller to process and store in the database without refreshing? 如何将表单数据发送到控制器以进行处理和存储在数据库中而无需刷新?

With Rails 3 and coming of the UJS, the simple fact of adding the remote attribute is sufficient to transform a request to AJAX. 随着Rails 3和UJS的到来,添加remote属性的简单事实足以将请求转换为AJAX。

But, for the request is handled as AJAX, this implies you have the Rails javascript library loaded. 但是,对于以AJAX处理的请求,这意味着您已加载Rails javascript库。

That means, you should include the rails.js file on your header. 这意味着,您应该在头文件中包含rails.js文件。

In your layout.erb.html, you should find 在您的layout.erb.html中,您应该找到

<%= javascript_include_tag :defaults %>

And in your public/javascripts directory, you should find : 在public / javascripts目录中,您应该找到:

application.js, controls.js, dragdrop.js, effects.js, prototype.js, rails.js

Then it should work. 然后它应该工作。

If in rails, you should make some file also in .js format and render it after save in controller. 如果在rails中,则还应该以.js格式制作一些文件,并在保存到控制器中后将其呈现。 For example render create.js after save in create controller. 例如在保存到创建控制器后渲染create.js。 In file create.js.erb, you should render back what you want to view after save that form 在文件create.js.erb中,保存该表单后,应重新呈现要查看的内容

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

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