简体   繁体   English

Rails表单,加载新的记录页面,而不是重定向到该页面

[英]Rails form, load new record page rather than redirecting to it

I'm creating my first app in rails. 我正在用Rails创建我的第一个应用程序。 Basically, I have a new customer form, normally when you enter a new customer you are redirected to the record you created. 基本上,我有一个新的客户表单,通常在您输入新客户时,您将被重定向到您创建的记录。

However as I am loading all my pages via ajax I want to load the new record in rather than re-direct to it. 但是,当我通过ajax加载我的所有页面时,我想加载新记录,而不是重定向到它。

I already have the form firing via ajax, I just need to know how I can access the new record URL to load it into my container. 我已经通过ajax触发了表单,我只需要知道如何访问新记录URL即可将其加载到我的容器中。

Hope that makes sense. 希望有道理。 Thanks in advance! 提前致谢!

You can add an option :remote => true to your form_for helper method, so that instead of page redirect the form gets posted via ajax. 您可以在form_for helper方法中添加一个:remote => true选项,以便通过Ajax发布表单而不是页面重定向。 For Ex : 对于Ex:

<%= form_for(@post, :remote => true) do |f| %>
  ...
<% end %>

Then create a new template named create.js.erb which will get rendered after create method has been executed. 然后创建一个名为create.js.erb的新模板,该模板将在执行create方法后呈现。

In this template, you can display the details of the new record you created. 在此模板中,您可以显示您创建的新记录的详细信息。

For Ex : 对于Ex:

$('some_element').replaceWith('<%=@posts.name %>');

Edit: You mentioned using load in your javascript. 编辑:您提到在JavaScript中使用load。 I would generally avoid this as you're making two round trips to the server. 我通常会避免这种情况,因为您要进行两次服务器往返。 1) Create 2) Get html to load into a div. 1)创建2)获取html以加载到div中。 Additionally, if there is an error that happens, it will be more difficult to catch and do the "good thing", whatever that might be. 此外,如果发生错误,无论发生什么事情,都将很难捕获并执行“好事”。

Once you've added the remote: true option to your form, you need to deal with what happens on the server side of things. 将remote:true选项添加到表单后,您需要处理服务器端发生的事情。

Assuming your using REST, you'll have a controller with a create action in it. 假设您使用REST,您将拥有一个带有create动作的控制器。 Normally, this method is creating the item and then subsequently returning the HTML for the create page. 通常,此方法创建项目,然后返回创建页面的HTML。 Instead of this, we need to create a javascript view for the action. 取而代之的是,我们需要为该操作创建一个javascript视图。 This will tell the calling page what to when this action is hit. 这将告诉调用页面该操作何时生效。

Let's assume your form has a div called "new_record_form" and that you have another div called "new_records". 假设您的表单具有一个名为“ new_record_form”的div,并且您还有另一个名为“ new_records”的div。 You'll want to blank out the form elements in the form, effectively resetting it. 您需要清空表单中的表单元素,以有效地对其进行重置。 You'll also want to add the new record to the "new_records" div. 您还将希望将新记录添加到“ new_records” div中。

To add the record to the new records div, you might do something like this. 要将记录添加到新记录div中,您可以执行以下操作。

$("#new_records").append("#{@new_record.name}");

When you submit the form, you should see this added. 提交表单时,您应该会看到添加的内容。 One great way to debug issues is to use the inspector. 调试问题的一种好方法是使用检查器。 If you're in chrome, right click anywhere, inspect element and select network. 如果您使用的是Chrome浏览器,请右键单击任意位置,检查元素并选择网络。 Do this prior to form submission. 在提交表单之前执行此操作。 You'll be able to see the ajax call and the response. 您将能够看到ajax调用和响应。 Your logs will also be helpful. 您的日志也将有所帮助。

Don't forget to blank out the form as well. 别忘了也将表格空白。

Note: You mentioned all your pages are ajax, but I highly suggest you evaluate if this makes 100% sense due to the various issues that result. 注意:您提到所有页面都是ajax,但是我强烈建议您评估由于各种问题而导致的100%合理性。 Not saying this is the best article on the subject but might be worth a read. 并不是说这是关于该主题的最佳文章 ,但可能值得一读。

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

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