jeudi 9 juillet 2015

Can't Sign Up User - Rails just refreshes my User New View

Im trying to make a new user and when i click submit it just refreshes the page. The submit button was working before but now it doesn't.

I've been trying to fix for hours now. Any ideas?

Here is my User controller.

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

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

  def show_user_items
    @user = User.find(current_user)
    @item = @user.items.paginate(page: params[:page])
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

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

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  private

    def user_params
      params.require(:user).permit(:username, :email, :password, :password_confirmation, :avatar, :description)
    end

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end

end

Here is my User new view:

<center><h1>Create Seller Account</h1></center>

<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= simple_form_for @user do |f| %>
            <%= f.input :username %>
            <%= f.input :email %>
            <%= f.input :password %>
            <%= f.input :password_confirmation %>
            <%= f.button :submit, "Create My Seller Account", class: "btn btn-primary" %>
          <% end %>
        </div>
      </div>    
    </div>
  </div>
</div>

Here is what my server says when i click submit

Started POST "/users" for ::1 at 2015-07-09 21:45:10 +1000
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"A8le1k27tk8fhpQ/T31V3iGFHzuXY1kspjnJBlMaWr9CxJ15S2C1QKCH7JgQ+wLlp/dz6O8CG9zX3nS60boLAQ==", "user"=>{"username"=>"Admin1234", "email"=>"admin@example.com.au", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create My Seller Account"}
   (0.2ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."username" = 'Admin1234' LIMIT 1
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'admin@example.com.au' LIMIT 1
   (0.1ms)  rollback transaction
  Rendered users/new.html.erb within layouts/application (12.7ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 323ms (Views: 222.5ms | ActiveRecord: 0.6ms)

Here is a test that is broken.

  4) Failure:
UsersSignupTest#test_valid_signup_information_with_account_activation [/Users/joseph/Documents/Safsy/Website/Safsy/Safsy/test/integration/users_signup_test.rb:22]:
"User.count" didn't change by 1.
Expected: 35
  Actual: 34

Here is the test like 22 is assert_difference 'User.count', 1 do:

  test "valid signup information with account activation" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, user: { username:  "Exampleuser16",
                               email: "user16@example.com",
                               password:              "password16",
                               password_confirmation: "password16" }
    end
    assert_equal 1, ActionMailer::Base.deliveries.size
    user = assigns(:user)
    assert_not user.activated?
    # Try to log in before activation.
    log_in_as(user)
    assert_not is_logged_in?
    # Invalid activation token
    get edit_account_activation_path("invalid token")
    assert_not is_logged_in?
    # Valid token, wrong email
    get edit_account_activation_path(user.activation_token, email: 'wrong')
    assert_not is_logged_in?
    # Valid activation token
    get edit_account_activation_path(user.activation_token, email: user.email)
    assert user.reload.activated?
    follow_redirect!
    assert_template 'users/show'
    assert is_logged_in?
  end

User Model:

class User < ActiveRecord::Base
  has_many :items
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :username, presence: true, length: { maximum: 50, minimum: 5 }, uniqueness: true
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true
  has_secure_password
  validates :description, presence: true, length: { maximum: 500 }
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
  has_attached_file :avatar, styles: { large: "250x250", medium:"100x100", small:"50x50", thumb:"30x30#"}
  validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]


  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random tocken.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

    # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Activates an account.
  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Converts email to all lower-case.
  def downcase_email
    self.email = email.downcase
  end

  # Creates and assigns the activation token and digest.
  def create_activation_digest
    self.activation_token  = User.new_token
    self.activation_digest = User.digest(activation_token)
  end

  # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

end

Aucun commentaire:

Enregistrer un commentaire