user3348545
user3348545

Reputation: 13

Rails sign-up form on home page

I seem to see questions on how I would be able to have my sign-up form for Users on the main page (static_pages#home) using devise, but couldn't find one for using just Rails.

I have this as my form on my home page:

  <h2>Sign Up</h2>
    <%= form_for(@user) do |f| %>
      <br>

      <%= f.label :email %>
      <%= f.text_field :email %><br>

      <%= f.label :password %>
      <%= f.password_field :password %><br>

      <%= f.label :password_confirmation, "Confirm password" %>
      <%= f.password_field :password_confirmation %><br>

      <%= f.submit "Sign up", class: "btn btn-large btn-primary" %>
    <% end %>

However, when I submit the form, the user data does not get saved (for some reason, the attributes of email and passwords are not taken from the form entries). I have instantiated the @user variable in the home action, so I'm not sure why this does not work.

class StaticPagesController < ApplicationController
    def home
        # For future where sign-up page is on home page.
        @user = User.new
    end
end

Any help would be appreciated. Thank you!

Upvotes: 1

Views: 248

Answers (2)

Rafael Ramos Bravin
Rafael Ramos Bravin

Reputation: 660

You can set the form target action by explicitly passing an url to it, doens't necessarly need to by the default #create action in UsersController.

So you could try that along with @Judy's answer

Upvotes: 0

Clinton
Clinton

Reputation: 2366

First, I would highly recommend using Devise. Handling user authentication is tricky business, especially trying to keep things secure.

Second, you need to have a controller action for the user to be saved. Something like:

class StaticPagesController < ApplicationController
  def create
    @user = User.new(params[:user])
    @user.save!
  end
end

And then add the right route to your config/routes.rb file.

Upvotes: 2

Related Questions