我试图将用户#new中的参数传递给用户#create with devise和rails 4,但我无法理解。我只是想获得@invitation,所以我可以在用户创建后编辑它。
class RegistrationsController < Devise::RegistrationsController
def new
@invitation = Invitation.find_by_invite_token(params[:invite_token])
redirect_to root_url if @invitation.nil
super
end
def create
@invitation = Invitation.find_by_invite_token(params[:invite_token])
# Devise create stuff:
build_resource(sign_up_params)
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
respond_with resource, :location => after_sign_up_path_for(resource)
else
...
@invitation在用户#create
中为零答案 0 :(得分:2)
因为#new使用POST提交的表单来到达#create,所以你的原始GET参数将不再在url中。你不能在没做任何事情的情况下在#create中获得令牌参数。
快速解决方法是在表单中添加一个邀请令牌字段。通过这个,您将@invitation
中的实例变量#new
转移到隐藏字段的值。当然,您也可以使用可见输入字段。
#views/devise/registrations/new
<%= form_for(resource..... %>
<%= f.hidden :invitation_token, value: @invitation_token
然后在#create中,您可以在以下内容中获得此参数:user
def create
@invitation = Invitation.find_by_invite_token(params[:user][:invite_token])