使用Rails 5上的两个按钮重定向到form_with

时间:2018-12-07 15:33:31

标签: forms ruby-on-rails-5

我在Rails视图中有一个form_with,它是通过另一个模型的局部视图呈现的。

该表单在其他模型中创建新记录,效果很好。问题是,除了创建记录外,我还希望有一个功能,该功能允许用户创建记录并在创建记录后进行重定向。

由于表单处理是远程的,因此show操作被呈现为JS并且未定向用户。

这是我目前的代码:

查看:

<%= form_with model: @model, url: another_model_controller_action_path do |f| %>
...
          <%= f.submit "Save", name: "save" %>
          <%= f.submit "Save and redirect", name: "save_and_redirect" %>
<% end %>

控制器:

def controller_action
if params[:save]
if @record.save
...
render "js partial"

elsif params[:save_and_redirect]
if @record.save
redirect_to record_path(@record)
end

有没有解决方法可以使两个按钮以相同的形式工作?

非常感谢

0 个答案:

没有答案