【发布时间】:2015-02-09 23:55:33
【问题描述】:
我创建了一个有一定长度的表单,为此我设置了一个 ajax 保存按钮,以便用户可以保存他们的进度并继续编辑表单。这很好用;没什么好抱怨的。
我的问题是,当用户完成编辑并想要进行最终提交时,我想在表单末尾添加第二个按钮。功能上的主要区别在于它在保存后重定向到“已提交表单,谢谢您的支持”页面。
我尝试过使用标准提交按钮 (<%= f.button :submit, "Submit", id:"incorporation_submit", class: "btn btn-primary" %>),但这似乎不起作用;单击它后,没有提交任何内容,也没有在日志中找到任何内容。 (我猜这是因为我已经将远程定义为适用于表单)。
我的表格概述如下;我删除了表单字段和侧边栏内容以尽量减少混乱。最后的提交按钮是上面显示的f.button :submit。
<div id="wrapper" class="active main-content">
<%= simple_form_for @incorporation, :remote => true do |f| %>
<div>
.
.
.
Form contents
.
.
.
</div>
<!-- Sidebar -->
<!-- Sidebar -->
<div id="sidebar-wrapper">
<ul id="sidebar_menu" class="sidebar-nav">
<li class="sidebar-brand"><a id="menu-toggle" href="#">Menu<span id="main_icon" class="glyphicon glyphicon-align-justify"></span></a></li>
.
.
.
Sidebar navigation tools
.
.
.
</ul>
<%= button_to "Save Progress", incorporation_path(@incorporation), remote: true, id:"save" %>
</div>
<div>
<%= f.button :submit, "Submit", id:"incorporation_submit", class: "btn btn-primary" %>
</div>
<% end %>
</div>
更新 1:
如果我从simple_form_for 中删除:remote=> true,提交按钮就可以正常工作,重定向和所有(显然ajax 保存按钮没有)
更新 2:
我发现如果我从侧边栏中删除button_to,我会从我的日志中得到一个输出;但是我无法成功地将浏览器重定向到我在控制器中设置的一个小静态页面。请参阅下面的输出:
Redirected to http://localhost:3000/welcome/index
Completed 302 Found in 134ms (ActiveRecord: 11.2ms)
Started GET "/welcome/index" for 127.0.0.1 at 2015-02-10 01:39:21 -0700
Processing by WelcomeController#index as JS
Rendered welcome/index.html.erb within layouts/application (0.2ms)
User Load (4.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Completed 200 OK in 1747ms (Views: 1740.3ms | ActiveRecord: 4.2ms)
【问题讨论】:
-
“似乎不起作用”是什么意思?请澄清您的错误或问题。
-
@dgilperez 感谢您的回复;公平的问题。我的意思是什么都没有提交,也没有在点击它的日志中找到任何东西。 (我在上面为未来的读者提供了这个解释)
-
您确定没有提交任何内容,即您检查过日志吗?浏览器控制台中的任何错误?
remote: true将需要服务器中的.erb.js文件来响应请愿书,这可能是缺失的。否则我在最后一个f.button :submit中看不到任何奇怪的东西。 -
另一个尝试:注释掉
button_to然后尝试提交。 -
@dgilperez 当我删除
button_to时,它确实在日志中产生了一些东西。但是我无法让它真正重定向(见上面的输出)。正如您所怀疑的那样,我没有任何.erb.js应该在我看来吗?这并没有阻止我使用 ajax 进行保存,所以也许我不知道去哪里找。
标签: javascript ruby-on-rails ajax forms ruby-on-rails-4