【问题标题】:Rails: Sending params to Create method with devise Rails 4Rails:使用设计 Rails 4 将参数发送到 Create 方法
【发布时间】:2013-10-17 16:54:17
【问题描述】:

我正在尝试使用 devise 和 rails 4 将参数从 user#new 传递给 user#create,但我无法弄清楚。我只想获得@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 在 user#create 中为零

【问题讨论】:

    标签: ruby-on-rails ruby devise ruby-on-rails-4


    【解决方案1】:

    因为#new 使用通过 POST 提交的表单到达 #create,所以您原来的 GET 参数将不再出现在 url 中。如果不做某事,您将无法在#create 中获取令牌参数。

    快速解决方法是在表单中添加一个邀请令牌字段。通过这种方式,您将#new 中的实例变量@invitation 转移到隐藏字段的值。当然你也可以使用可见的输入框。

    #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])
    

    【讨论】:

    • 您的绅士和学者。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-05-06
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2011-12-18
    • 1970-01-01
    相关资源
    最近更新 更多