mardi 5 mai 2015

Ruby rails can't add user entry

Everything seems to go fine and there's no error message when executing user.save, but it doesn't show up when indexing.

I ran the migration:

class AddEmailToUsers < ActiveRecord::Migration
  def change
    add_column :email, :name, :password, :password_confirmation, :string
  end
end

models/user.rb

class User < ActiveRecord::Base
  #attr_accessor :remember_token
  attr_accessor :email
  attr_accessor :name
  attr_accessor :password_digest
  attr_accessor :password, :password_confirmation
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  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: { case_sensitive: false }
 # has_secure_password
  validates :password, length: { minimum: 6 }

  # functions...
end

Here on create I fill in some sample values to test it, and it submits to views/users/create.html.erb without anything displaying in "Error: []" and the correct data shown for the fields of @user.

controllers/user_controller.rb

class UsersController < ApplicationController
  def new
    #require 'bcrypt'
    @user = User.new
  end

  def index
    @users = User.all
  end

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

 def create
    #@user = User.new(user_params)
    @user = User.new(name: "di3", email:"di3@di3.com", password: "di3", password_confirmation: "di3")
    #@user = User.new(params[:user])
    if @user.save!
      log_in @user
      flash[:success] = "Welcome to the forum!"
      #redirect_to @user
      #redirect_to users_path
    else
      #render 'new'
      render @user.error_messages.full
    end
  end

  private

  def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

views/users/create.html.erb

<h1>Create user params</h1>

<%= render partial: 'layouts/header' %>

<table>
  <tr>
    <th>Email</th>
    <th>Name</th>
    <th>Password</th>
  </tr>

      <tr>
        <td><%= @user.email %></td>
        <td><%= @user.name %></td>
        <td><%= @user.password %></td>
        <td><%= @user.password_confirmation %></td>
      </tr>
</table>

<br/>

Error:

<%= @user.errors.full_messages %>

But this user index doesn't show any users at all:

views/users/index.html.erb

<h1>Listing forums</h1>

<%= render partial: 'layouts/header' %>

<table>
  <tr>
    <th>Email</th>
    <th>Name</th>
    <th>Password</th>
    <th>Password</th>
    <th>Password</th>
  </tr>

  <% if(@users)
    @users.each do |user| %>
      <tr>
        <td><%= user.email %></td>
        <td><%= user.name %></td>
        <td><%= user.password_digest %></td>
        <td><%= user.password %></td>
        <td><%= user.password_confirmation %></td>
      </tr>
    <% end %>
  <% end %>
</table>

Aucun commentaire:

Enregistrer un commentaire