this is my first question here and I am currently trying to build my first Ruby on Rails app so this question might be kind of basic.
The Problem:
I want a form on the root site which consists of only two fields, email and password for a user model. Depending on whether the email address that was typed in already exists or not, the form should forward the request to the 'users#create' (i.e. create a new user) or the 'sessions#create' (i.e. Sign the user in ... if the password is correct)
What I tried:
To solve this I created a new action in the pages controller called 'decide' and pointed the form to that page.
It basically looks like this:
def decide
if User.find_by_email(params[:email])
# Copied in all of the code from sessions#create
# with a redirect at the end so a /decide page doesn't get rendered
else
# Copied in all of the code from users#create
# with a redirect at the end so a /decide page doesn't get rendered
end
end
--
Now this works and I'm pretty proud of myself that I figured it out as a complete beginner, but I can hear experienced programmers crying out in agony at this code. I'm pretty sure this is not the "Rails Way".
I repeat a large amount of code and I feel like I'm putting something in a controller that wasn't made for that purpose.
Another problem is that I can't get my rspec integration test to work, which basically goes to the root_path, fills in the form with a new user, hits the button and expects a success flash... but the test gets returned an empty html document on pressing the button. Although it works perfectly fine when I do the same thing in a browser.
So I figured:
Couldn't you somehow put the above if statement in the routes.rb file and redirect the request to the respective controller and action?
If so how?
Or does anyone have a different idea how to accomplish this?
Thank you,
-Konstantin