我有一種情況。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')可以做到這一點。我只是從數據庫中提取信息。但我需要會話信息來堅持我的登錄和會話處理程序。有什麼想法嗎? –