3

I have index method that displaying two forms Sign in and Sign up, that means user can create an account and a log in from same place.

so I have users controller with index method that displaying a view with Sign in and Sign up form with two partials one is _signin.html.erb and _signup.html.erb in index.html.erb.

Any Idea How can I handle new and create methods from users and sessions controllers (may be I can ignore new method)?

3
  • Are you still having problems with this? Asking because you unaccepted my answer. Commented Jun 7, 2013 at 22:51
  • @amesee - see reddit.com - register page, both sign in and sign up on same page, I am looking something like that Commented Jun 7, 2013 at 23:36
  • That's just a matter of showing both forms on the same page. One for signing in and another for signing up, where each form sends requests to sessions/create or users/create respectively. Both answers on this page pretty much explain how to do that. Are you just not familiar enough with rails to understand this? I mean the problem of user authentication and user creation is pretty much solved in almost every intro to rails book and tutorial. Commented Jun 8, 2013 at 2:17

3 Answers 3

4

As long as each form is being rendered with the correct model object and/or the correct value to the :url option, each form should send the expected request (assuming you're rendering your forms with form_for).

For example, your sign in form should start with something like this::

<%= form_for :session, :url => sessions_path %>
  #...
<% end %>

As long as a POST request (the default from form submissions) is sent to a "collections" resource (i.e. /sessions) it will route the request to the create action in your SessionsController or whatever you named your controller.

For signing up, you probably have something like this:

<%= form_for @user do |f| %>
  # ...
<% end %>

The @user model object will assume the request should go to /users. Again this will call your create action in your UsersController.

Of course, all this is also assuming your config/routes.rb file is just declaring each resource with something like:

resources :users
resources :sessions, :only => [:create, :destroy]

You usually have each form in separate views such as

match 'sign_in', :to => 'sessions#new'
match 'sign_up', :to => 'users#new'

But if you only want to display these forms in an index.html.erb view then these routes are no longer necessary.

Sign up to request clarification or add additional context in comments.

Comments

3
<%= form_for :signin,:url=>{:controller=>"yourcontroller",:action=>"signin"},:html=>{:id=>"signin_form"} do |f|%>
  ...
<%end%>

<%= form_for :signup,:url=>{:controller=>"yourcontroller",:action=>"signup"},:html=>{:id=>"signup_form"} do |f|%>
...
<%end%>

Comments

0

One simple workaround that I found was that if the two forms have at least one uniquely named parameter, then you can simply route the POST request to a single action. Then within the action check which parameter exists and execute the corresponding code. You end up having essentially two actions within one action in your controller.

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

Just be sure to have it configured in your routes.rb so that when a POST request comes from the page the two forms are on, it will direct to this action in this controller.

Hope this helps!

1 Comment

I think this is a very, very bad idea: it is super inflexible as well as error-prone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.