Ruby on rails 创建rails对象与服务单独的请求并行
我正试图找到一个平静的方式,使我的注册确认过程顺利进行。我正在使用Rails 4和Desive 3.0。工艺流程如下所示: 用户注册->发送确认电子邮件->用户单击确认电子邮件链接->用户被定向到lead控制器,在那里它生成lead(用户的空白页面约2秒)->然后用户被重定向到其仪表板 当前的流程就是不流动。当用户点击确认他们的电子邮件时,他们会被带到一个空白页面,在那里我提交一个隐藏表单,用他们的信息创建一个lead对象,一旦提交了隐藏表单,仪表板页面就会被加载,这非常混乱。URL会在几秒钟后发生变化,这会让用户感到困惑 我想在他们单击确认电子邮件链接后创建lead对象。我有一个当前的流程,通过leads_controller new和create方法路由用户,然后自动将其定向到仪表板。我正在使用designe sign_in_count属性来确保这是他们第一次登录。如果这是他们第一次登录,我会引导他们通过leads控制器生成一个包含他们信息的lead 一旦用户选择电子邮件确认链接,我将在加载仪表板页面时生成lead对象 应用程序\u控制器.rbRuby on rails 创建rails对象与服务单独的请求并行,ruby-on-rails,rest,ruby-on-rails-4,devise,Ruby On Rails,Rest,Ruby On Rails 4,Devise,我正试图找到一个平静的方式,使我的注册确认过程顺利进行。我正在使用Rails 4和Desive 3.0。工艺流程如下所示: 用户注册->发送确认电子邮件->用户单击确认电子邮件链接->用户被定向到lead控制器,在那里它生成lead(用户的空白页面约2秒)->然后用户被重定向到其仪表板 当前的流程就是不流动。当用户点击确认他们的电子邮件时,他们会被带到一个空白页面,在那里我提交一个隐藏表单,用他们的信息创建一个lead对象,一旦提交了隐藏表单,仪表板页面就会被加载,这非常混乱。URL会在几秒钟后
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def account_url
return new_user_session_url unless user_signed_in?
case current_user.class.name
when "Business"
business_root_path
when "Lender"
lender_root_path
else
root_path
end if user_signed_in?
end
def after_sign_in_path_for(resource)
if resource.sign_in_count == 1
new_lead_path
else
stored_location_for(resource) || account_url
end
end
end
class LeadsController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def new
@lead = Lead.new
end
def create
@lead = Lead.new(params[:lead])
@lead['RecordTypeId'] = 'XXXXX'
@lead['OwnerId'] = 'XXXXX'
@lead['FirstName'] = "XXXXXX"
@lead['LastName'] = "XXXXX"
@lead['Company'] = "XXXXX"
@lead['Email'] = current_user.email
@lead['IsConverted'] = false
@lead['IsUnreadByOwner'] = true
if @lead.save
redirect_to lender_root_path
end
end
end
class LenderAccountController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def dashboard
render layout: 'simple'
@leads = Lead.all
end
end
def account_url
return new_user_session_url unless user_signed_in?
current_user.root_path
end
class ApplicationController
leads\u controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def account_url
return new_user_session_url unless user_signed_in?
case current_user.class.name
when "Business"
business_root_path
when "Lender"
lender_root_path
else
root_path
end if user_signed_in?
end
def after_sign_in_path_for(resource)
if resource.sign_in_count == 1
new_lead_path
else
stored_location_for(resource) || account_url
end
end
end
class LeadsController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def new
@lead = Lead.new
end
def create
@lead = Lead.new(params[:lead])
@lead['RecordTypeId'] = 'XXXXX'
@lead['OwnerId'] = 'XXXXX'
@lead['FirstName'] = "XXXXXX"
@lead['LastName'] = "XXXXX"
@lead['Company'] = "XXXXX"
@lead['Email'] = current_user.email
@lead['IsConverted'] = false
@lead['IsUnreadByOwner'] = true
if @lead.save
redirect_to lender_root_path
end
end
end
class LenderAccountController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def dashboard
render layout: 'simple'
@leads = Lead.all
end
end
def account_url
return new_user_session_url unless user_signed_in?
current_user.root_path
end
class LeadsController
new.html.erb(这是提交以创建新lead对象的隐藏表单
<div class="container content">
<%= form_for @lead do |f| %>
<div class="field">
<%= f.hidden_field :LeadSource, :value => 'Lending Loop' %>
<%= f.hidden_field :Status, :value => 'Registered' %>
</div>
<div class="actions">
<%= f.submit :id => "hidden-submit", :style => "display: none;" %>
</div>
<% end %>
</div>
<script>
$(function () {
$('#hidden-submit').click();
});
</script>
'借贷循环'%>
'已注册'%>
“隐藏提交”,“样式=>”显示:无;“%>
$(函数(){
$(“#隐藏提交”)。单击();
});
贷方账户控制器.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def account_url
return new_user_session_url unless user_signed_in?
case current_user.class.name
when "Business"
business_root_path
when "Lender"
lender_root_path
else
root_path
end if user_signed_in?
end
def after_sign_in_path_for(resource)
if resource.sign_in_count == 1
new_lead_path
else
stored_location_for(resource) || account_url
end
end
end
class LeadsController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def new
@lead = Lead.new
end
def create
@lead = Lead.new(params[:lead])
@lead['RecordTypeId'] = 'XXXXX'
@lead['OwnerId'] = 'XXXXX'
@lead['FirstName'] = "XXXXXX"
@lead['LastName'] = "XXXXX"
@lead['Company'] = "XXXXX"
@lead['Email'] = current_user.email
@lead['IsConverted'] = false
@lead['IsUnreadByOwner'] = true
if @lead.save
redirect_to lender_root_path
end
end
end
class LenderAccountController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def dashboard
render layout: 'simple'
@leads = Lead.all
end
end
def account_url
return new_user_session_url unless user_signed_in?
current_user.root_path
end
class LenderAccountController
除非我这里遗漏了什么,如果隐藏表单总是发布相同的数据,为什么有必要提交它呢?在您的模型中创建回调之前,可以在中设置LeadSource和状态。因此,您在创建操作中的所有行为都来自@lead['RecordTypeId']='XXXXX'
到@lead['IsUnreadByOwner']=true
也属于模型中的回调
在我看来,流程应该是:
用户注册->发送确认电子邮件->用户单击确认电子邮件链接->用户被定向到leads controller创建
操作,在该操作中,它在后端处理lead generation->创建
操作重定向到仪表板。他们从不访问leads\u controller.rb中的新建
操作,但您可能会希望将其保留在那里,以备将来需要。您的创建操作可以很简单:
def create
@lead = Lead.new
if @lead.save
redirect_to lender_root_path
end
end
然后将所有在模型上设置值的活动移动到lead.rb
中的私有方法,在创建回调之前或在创建过程中您认为合适的任何位置运行它
您也可以完全跳过lead控制器,并在UsersController上的“验证”操作中对此进行处理。用户单击链接,帐户被验证,相同的lead创建操作可能会发生。我想应该提到的是,在用户单击链接(GET请求)后创建对象将被视为不正确地使用REST,因此,如果您想在语义上正确,您应该将他们引导到一个页面,在该页面上他们会单击一个按钮来实际执行最终验证。这不会是无缝的,您需要权衡对标准的遵守情况与您想要创建的用户体验。不过,基本情况并没有改变:所有对象都是c创建应该在后端完成,而无需将用户指向空白页
由于我们谈论的是混乱的事情发生,因此控制器中有许多逻辑发生,您应该避免。特别是ApplicationController中的案例
存在依赖性问题,这些问题可能会在将来给您带来问题。请从这样做开始:
应用程序\u控制器.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def account_url
return new_user_session_url unless user_signed_in?
case current_user.class.name
when "Business"
business_root_path
when "Lender"
lender_root_path
else
root_path
end if user_signed_in?
end
def after_sign_in_path_for(resource)
if resource.sign_in_count == 1
new_lead_path
else
stored_location_for(resource) || account_url
end
end
end
class LeadsController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def new
@lead = Lead.new
end
def create
@lead = Lead.new(params[:lead])
@lead['RecordTypeId'] = 'XXXXX'
@lead['OwnerId'] = 'XXXXX'
@lead['FirstName'] = "XXXXXX"
@lead['LastName'] = "XXXXX"
@lead['Company'] = "XXXXX"
@lead['Email'] = current_user.email
@lead['IsConverted'] = false
@lead['IsUnreadByOwner'] = true
if @lead.save
redirect_to lender_root_path
end
end
end
class LenderAccountController < ApplicationController
before_filter :authenticate_user!
include Databasedotcom::Rails::Controller
def dashboard
render layout: 'simple'
@leads = Lead.all
end
end
def account_url
return new_user_session_url unless user_signed_in?
current_user.root_path
end
贷款人模式
def root_path
lender_root_path
end
def root_path
business_root_path
end
商业模式
def root_path
lender_root_path
end
def root_path
business_root_path
end
根路径.rb模块
module RootPath
def root_path
root_path
end
end
然后在所有其他需要响应根路径的模型中,只要包含根路径
,该方法就会可用。之后,您只需调用当前用户.root路径
,它就会工作。感谢您的响应。我没有潜在客户模型,因为我使用Databasedotcom gem使用Salesforce来存储我的领导对象,这就是为什么我