私が見つけた 1 つの簡単な回避策は、2 つのフォームに少なくとも 1 つの一意の名前のパラメーターがある場合、POST 要求を単一のアクションに単純にルーティングできるというものでした。次に、アクション内でどのパラメーターが存在するかを確認し、対応するコードを実行します。コントローラーの 1 つのアクション内に本質的に 2 つのアクションが存在することになります。
def create
if params[:username] and !params[:name]
# You know that the user pressed submit on whichever form
# has a field that fills params[:username].
# So do the action with that form's parameters here
# i.e, login an existing user
respond_to do |format|
format.html {redirect_to '/home', notice: "Login successful"}
end
elsif params[:name] and !params[:username]
# You know that the user pressed submit on whichever form
# has a field that fills params[:username].
# So do the action with that form's parameters here
# i.e, create a new user
respond_to do |format|
format.html {redirect_to '/onboard', notice: "Thanks for signing up"}
end
end
end
2 つのフォームがオンになっているページから POST リクエストが送信されたときに、このコントローラのこのアクションに送信されるように、routes.rb で必ず設定してください。
お役に立てれば!