I have followed the examples in Chapter 11 page 147-155 and can’t get the ’ Create User ’ to work. I’m new to ruby so maybe there is a syntax error or something. Here is what I have and the error. Please let me know if you see any mistakes.
Here is the error.
NoMethodError in Login#add_user
Showing app/views/login/add_user.rhtml where line #10 raised:
undefined method `password’ for #User:0x2aa0abc Extracted source (around line #10):
7:
8:9: Password: 10: <%= form.password_field :password, :size => 40%> 11:
12: Confirm: 13: <%= form.password_field :password_confirmation, :size => 40%>
… Than the code …
LOGIN CONTROLLER …
class LoginController < ApplicationController
layout “admin”
def add_user @user = User.new(params[:user]) if request.post? and @user.save flash.now[:notice] = “User #{@ user.name} created successfully!” @user = User.new end end
USER.RB
require “digest/sha2”
class User < ActiveRecord::Base validate_presence_of :name validates_uniqueness_of :name
attr_accessor :password_confirmation validates_confirmation_of :password
def validate errors.add_to_base(“Missing Something”) if hashed_password.blank?
end
def self.authenticate(name, password) user = self.find_by_name(name) if user expected_password = encrypted_password(password, user.salt) if user.hashed_password != expected_password
user = nil
end
end
user
end
def password @password end
def password=(pwd) @password = pwd create_new_salt self.hashed_password = User.encrypted_password(self.password, self.salt) end
private
def self.encrypted_password(password, salt) string_to_hash = password + “bobblehead” + salt #bobblehead to random the pass for security
Digest::SHA256.hexdigest(string_to_hash)
def create_new_salt self.salt = self.object_id.to_s + rand.to_s end end end
ADD_USER.RHTML
Create User<% form_for :user do |form| %>
<p>
<label for="user_name">Name:</label>
<%= form.text_field :name, :size => 40 %>
</p>
<p>
<label for="user_password">Password:</label>
<%= form.password_field :password, :size => 40%>
</p>
<label for="user_password_confirmation">Confirm:</label>
<%= form.password_field :password_confirmation, :size => 40%>
</p>
<p>
<%= submit_tag "Add User", :class => "submit" %></p>
<% end %>