-1

我有一个情况。

>rails -v
Rails 4.2.4

>ruby -v
ruby 2.1.8p440 (2015-12-16 revision 53160) [i386-mingw32]

我正在使用Apartment gem构建多租户时间输入解决方案。这是流程。我被困住了。

1) 用户到达创建租户的注册页面。这是控制器:

  def new
    @tenant = Tenant.new
  end

  def create
    @tenant = Tenant.new(tenant_params)
    session[:tenant_attributes] = @tenant.attributes

    if @tenant.save

      flash[:success] = "Org ID #{session[:tenant_attributes]["org_identifier"]} created for #{session[:tenant_attributes]["org_name"]} successfully!"
      # Start a job for creating the tenant: this is handled in the model?
      # Apartment::Tenant.switch!("#{session[:tenant_attributes]["org_identifier"]}")
      redirect_to new_user_url(:subdomain => "#{session[:tenant_attributes]["org_identifier"]}")  #new_user_path
    else
      render :new
    end
  end

和观点:

<% provide(:title, 'Sign up') %>
<h1>Welcome!</h1>
<p class="center">
  Make time entry easy, for once.
</p>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for (@tenant) do |f| %>
        <%= render 'shared/tenant_error_messages' %>

        <%= f.label :creator_email, "What is your email address?" %>
        <%= f.email_field :creator_email, class: 'form-control' %>

        <%= f.label :org_name, "What is the name of your organization?" %>
        <%= f.text_field :org_name, class: 'form-control' %>

        <%= f.label :org_identifier, "Please choose an abbreviated Org ID" %>
        <%= f.text_field :org_identifier, class: 'form-control' %>

        <%= f.submit "Create your own Org", class: "btn btn-primary" %>

    <% end %>
  </div>
</div>

如您所见,这被设置为重定向到new_user_url助手传递一个等于新 org_identifier(刚刚创建的租户)的子域。

2) 提交正确的信息后,重定向发生在租户创建后。我已经确认创作正在发生。我已经确认重定向是正确的。然后会话变量清空自己。

用户控制器:

def new
    # Instantiate a new user object at the open of the 'new' view
    @user = User.new

  end

 def create
    # This is the method conducted at the submission of the form and instantiates/saves its own user object
    @user = User.new(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Time Entry Solution!"
      if @user.site_id == "nil"
        redirect_to new_client_path #(:subdomain => session[:tenant_attributes]["org_identifier"])
      else
        redirect_to current_user
      end

    else
      render 'new'
    end
  end

用户/新视图:

<% provide(:title, 'Sign up') %>
<center><p>
  Tell us more about you.
</p>
</center>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
  <% if session[:tenant_attributes] %>

    <h2><%= session[:tenant_attributes] %></h2>
    <h2><%= session[:tenant_attributes] %></h2>
        <% else %>
    <h2> The session variable is empty.</h2>
<% end %>

    <%= form_for (@user) do |f| %>
        <%= render 'shared/error_messages' %>

        <%= f.hidden_field :email, :value => session[:tenant_attributes]["creator_email"] %>

        <%= f.label :first_name %>
        <%= f.text_field :first_name, class: 'form-control' %>

        <%= f.label :last_name %>
        <%= f.text_field :last_name, class: 'form-control' %>

        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control' %>

        <%= f.label :password_confirmation, "Confirmation" %>
        <%= f.password_field :password_confirmation, class: 'form-control' %>

        <%= f.submit "Create an org ID", class: "btn btn-primary" %>

    <% end %>

  </div>
</div>

当我提交后续表单时 - 其中包括一个隐藏字段,其中电子邮件参数作为我想要的会话值 - 我得到“电子邮件无效”。

--- !ruby/hash:ActionController::Parameters
utf8: "✓"
authenticity_token: IAEe2EyvrGFa9gMp2X0WnlBzMHF9WdDXhVIs5s3vPkK54xqgICp3T8S/tXQiQnKYqr2CVTH7+0H9G4SaZNGoDQ==
user: !ruby/hash:ActionController::Parameters
  email: ''
  first_name: Anthony
  last_name: Gardner
  password: Password12345!
  password_confirmation: Password12345!
commit: Create an org ID
controller: users
action: create

我浏览了 Apartment gem 文档并搜索了网络。我看不出这应该失败的任何原因。但当然,我确信它就在我面前。任何帮助,将不胜感激。

更新:

我找到了我将尝试的解决方案,将 cookie 存储的域设置为 all,然后在应用程序控制器内部进行检查。我没有使用设计,但我们会看看它是否有效。

4

1 回答 1

0

在这里找到答案:Share session (cookies) between subdomains in Rails?

问题出在我们的 session_store.rb 中。我们没有考虑到通过设置domain: :all,我们忽略了默认的 TLD 长度 1。我们使用的是 lvh.me 而不是 localhost。

希望这对其他人有帮助!

于 2016-05-02T20:43:37.657 回答