jeudi 26 mai 2016

Devise with Rails Not Creating Users Properly

I'm a Rails beginner, and I am trying to make a site with a signup page that creates users in a database I set up. I installed devise (seemingly) correctly, and after creating the first user to test it out, everything seemed to work. However, if I try creating another user, it doesn't work. After clicking create user on the page, it goes back to the homepage without any errors or anything. But when I check the users table in my database, I can see the user was not created.

I got it to work when I changed the first users id (primary key) from 2 to 1. And the new user's id was 3. So it looks like for whatever reason, devise wants to skip a step, or is somehow trying to insert twice or something. But once again, even after I got the new user to create, if I try again it doesn't work.

Here's a printout of the rails server during the create request:

Started POST "/users" for ::1 at 2016-05-26 16:08:26 -0400
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zl39rAOLLSRoEZUIcX574BlJtNgySlz/d5FRNIg3ZjuvcdQJxOVLDdWPAp3jQqhT3g1H0PCexFdHdmAQD81CKQ==", "user"=>{"first_name"=>"asdasd", "last_name"=>"asdasd", "email"=>"asdasd@asda.com", "password"=>"[FILTERED]", "confirm_password"=>"[FILTERED]"}, "instructor"=>"1", "commit"=>"Create my account"}
  User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 4  ORDER BY `users`.`id` ASC LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 39ms (ActiveRecord: 0.5ms)

Here is my UsersController file:

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def edit
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
    @user.active = 1
  end

   def create
     @user = User.new(sign_up_params)    # Not the final implementation!
     if @user.save
       redirect_to :action => :index
     else
       render 'signup'
     end
   end

  def destroy
    @user = User.find(params[:id]).destroy
    if @user.destroy
      redirect_to users_path
    else
      render 'index'
    end
  end

 private

   def user_params
     params.require(:user).permit(:first_name, :last_name, :email, :instructor, :active)
   end

end

And here is my new.html.erb file:

<h1>New User</h1>

<%= render 'form' %>

<%= link_to 'Back', users_path %>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
      <%= f.label :first_name %>
      <%= f.text_field :first_name %>

      <%= f.label :last_name %>
      <%= f.text_field :last_name %>

      <%= f.label :email %>
      <%= f.email_field :email %>

      <%= check_box_tag(:instructor) %>
        <center><%= label_tag(:instructor, "I want to be an instructor") %></center>
      <br>
      <br>
      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :confirm_password %>
      <%= f.password_field :confirm_password %>

      <%= f.submit "Create my account", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

Thanks for any help that you can provide

Aucun commentaire:

Enregistrer un commentaire