我試圖實現同樣的概念瑞安貝茨在他的Railscast在這裏討論:http://railscasts.com/episodes/217-multistep-forms到我的用戶註冊表單。多步表單,但重定向到另一個網站?
基本上3個步驟如下:
步驟1.填寫用戶細節
步驟2:轉到外部URL來完成計費細節,這將它們重定向回此用戶在完成後註冊表單。
第3步:確認並提交數據
目前我的解決方法是,首先創建用戶,然後重定向到外部URL完成後@ user.save付款,但我想用這個多步表單,以防止用戶在未完成付款時被保存。
如果你能指出我朝着一個方向,我將不勝感激。謝謝。
UPDATE:
我的用戶控制器:
def new
session[:user_params] ||= {}
if params[:plan_id].present?
@user = User.new(session[:user_params] && :plan_id => params[:plan_id])
else
@user = User.new(session[:user_params])
end
@user.current_step = session[:user_step]
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
def create
session[:user_params].deep_merge!(params[:user]) if params[:user]
@user = User.new(session[:user_params])
@user.current_step = session[:user_step]
if @user.valid?
if params[:back_button]
@user.previous_step
elsif @user.last_step?
@user.save
elsif @user.billing_step?
@user.next_step
redirect_to("external.com") and return
else
@user.next_step
end
session[:user_step] = @user.current_step
end
if @user.new_record?
render "new"
else
session[:user_step] = session[:user_params] = nil
flash[:success] = "User saved"
redirect_to dashboard_url
end
end
我的用戶模型:
validates_presence_of :username, :first_name, :last_name, :if => lambda { |u| u.current_step == "initial" }
attr_writer :current_step
def current_step
@current_step || steps.first
end
def steps
%w[initial billing confirmation]
end
def next_step
self.current_step = steps[steps.index(current_step)+1]
end
def previous_step
self.current_step = steps[steps.index(current_step)-1]
end
def first_step?
current_step == steps.first
end
def last_step?
current_step == steps.last
end
def billing_step?
current_step == steps.second
end
我的新用戶視圖:
<%= form_for @user, :url => {:action => :create, :plan_id => params[:plan_id] } do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render "#{@user.current_step}_step", :f => f %>
<% end %>
的問題是在用戶控制器與@user .billing_step?
點擊提交按鈕後我需要發生的事情是:1.)添加一個步驟(使用@ user.next_step)和2.)重定向到一個外部URL。
這樣,當用戶返回「用戶/新」時,他們已經處於確認的最後一步。
如果我在重定向結束時沒有添加「and return」命令,則會出現「在此操作中多次調用渲染和/或重定向」錯誤。如果我這樣做,Rails不會添加新的步驟,並將我帶回到整個事件的結算步驟(步驟2)。
這是其他問題之一;該服務在完成付款後不會返還任何付款金額。您只需指定處理付款後用戶重定向到的URL。對我來說,這是我的「用戶/新」 - 這個想法是,你點擊「繼續付款」按鈕,1.)將您帶到下一步,並且2.)將瀏覽器重定向到外部站點。我嘗試添加一個'billing_step',正如我在OP中提到的那樣,但是我得到了「太多重定向」的錯誤,除非我在結尾處放置了'並返回' - 如果我這樣做,它不需要用戶到下一步。 – cdotfeli 2012-01-05 11:13:54
好的,那麼你不需要檢查付款細節,以防他們只在付款處理後重定向到網址,是的?你可以在重定向時定義它嗎?如果是這樣,您可以爲待註冊的用戶創建一個唯一的ID,並讓返回的URL具有該ID。這當然可以像你第一次描述的那樣完成,首先保存用戶,但是處於「不活躍」狀態,然後在處理付款後激活。奇怪的服務如何不包含任何信息.. – 2012-01-05 11:21:58
你可以告訴我們的代碼,它會更容易看到你正在使用這樣做的邏輯。 – 2012-01-05 11:22:26