用于HTTP Post的Rails 4 before_action

时间:2014-06-09 23:10:57

标签: ruby-on-rails post

我在创建方法上有一个之前的操作,如下所示:

class ApplicationController
  before_action :authenticate_user!
end

class PostsController < ApplicationController
  def create
    @post = Post.new(post_params)

    if @post.save
      redirect_to post_path(@post)
    else
      redirect_to root_path
    end
  end
end

#on some view while logged out, a user may see a form like so
<%= form_for :post, url: posts_path, method: :post do |f| %>
  # some post data
  <%= f.submit %>
<% end %>

我希望能够成为已注销的用户并查看允许访客发布的表单。当访客单击表单提交按钮时,我希望它呈现登录表单,并在成功登录后继续进行创建方法。

所有工作都在进行,直到进入帖子#create方法。相反,它正在渲染root_path。我不确定这是否是before_action所需功能的一部分。

那么,是否有可能让before_action继续使用表单中原始参数的HTTP Post方法?

1 个答案:

答案 0 :(得分:0)

如果您使用的是设计,可以在ApplicationController

中添加这样的内容
def after_sign_in_path_for(resource)
    redirect_to request.referrer
end

这将确保每次登录后,您都会参考上一页。