我有一个情况。
>rails -v
Rails 4.2.4
>ruby -v
ruby 2.1.8p440 (2015-12-16 revision 53160) [i386-mingw32]
我正在使用the 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>
当我提交后续表单 - 其中包含一个带有email参数的隐藏字段作为我想要的会话值时 - 我收到“电子邮件无效”。
--- !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 doc并浏览了网页。我看不出为什么这会失败的原因。但当然,我确信它正好在我面前。任何帮助将不胜感激。
更新:
我发现我将尝试this solution,将cookie存储区域设置为all,然后在app控制器内部进行检查。我没有使用设计,但我们会看看它是否有效。
答案 0 :(得分:0)
在这里找到答案:Share session (cookies) between subdomains in Rails?
问题出在我们的session_store.rb中。我们没有考虑到通过设置domain: :all
,我们忽略了默认TLD长度1.我们使用的是lvh.me而不是localhost。
希望这有助于其他人!