我有一种情况。Rails - 会话变量不能通过公寓开关持续存在
>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)正确提交信息后,重定向发生在创建租户后。我已确认创作正在发生。我已确认重定向是正确的。然后会话变量清空它自己。
users控制器:
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 doc并浏览了网页。我看不出有什么理由为什么这会失败。但是,当然,我确定这是正确的。任何帮助,将不胜感激。
UPDATE:
我发现this solution,我将设法,设置cookie存储的域名,然后所有的应用程序控制器内部检查之后。我没有使用设计,但我们会看看它是否有效。
通过更新,他们推荐的解决方案('protect_from_forgery with::null_session')可以做到这一点。我只是从数据库中提取信息。但我需要会话信息来坚持我的登录和会话处理程序。有什么想法吗? –