仅当用户未登录时才使用远程表单 Rails 4

remote form only if user isn't signed in Rails 4

我只想让未登录的用户使用带有 remote: true 的表单,但同时 signed_in 用户应该被正常重定向。 所以我尝试了类似的方法,但它不起作用(甚至看起来都不正确)但你明白了:

<%= simple_form_for(@post, html: {class: "form-horizontal"}) if user_signed_in? do |f| %>
<%= simple_form_for(@post, html: {class: "form-horizontal"}, remote: true) if !user_signed_in? do |f| %>

您的解决方案不起作用,因为您对 simple_form_for 的第二次调用位于您刚刚传递给 simple_form_for 的第一次调用的块中。您可以只使用三元运算符将 remote 选项设置为 true 或 false,如下所示:

<%= simple_form_for(@post, html: {class: "form-horizontal"}, remote: ( user_signed_in? ? false : true ) ) do |f| %>