我在名为Admin的rails上的ruby中创建了一个自定义模块。但是在注册时我无法保存表单数据(姓名,电子邮件,密码)。请帮助我......
用户控制器
module Admin
class UsersController < ApplicationController
before_filter :authenticate, :except => [:show, :new, :create]
before_filter :correct_user, :only => [:edit, :update]
before_filter :admin_user, :only => :destroy
def index
@title = "All users"
# @users = User.all
@users = User.paginate(:page => params[:page])
end
def new
@title = "Sign up"
@user = User.new
end
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(:page => params[:page])
@title = @user.name
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
@title = "Sign up"
render 'new'
end
end
def edit
# @user = User.find(params[:id])
@title = "Edit user"
end
def update
# @user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated."
redirect_to @user
else
@title = "Edit user"
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_path
end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.following.paginate(:page => params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(:page => params[:page])
render 'show_follow'
end
private
#10.2
def current_user?(user)
user == current_user
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def admin_user
redirect_to(root_path) unless current_user && current_user.admin?
end
end
end
用户模型
module Admin
require 'digest'
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :microposts, :dependent => :destroy
has_many :relationships, :foreign_key => "follower_id",
:dependent => :destroy
has_many :following, :through => :relationships, :source => :followed
has_many :reverse_relationships, :foreign_key => "followed_id",
:class_name => "Relationship",
:dependent => :destroy
has_many :followers, :through => :reverse_relationships, :source => :follower
has_many :replies, foreign_key: "to_id", class_name: "Micropost"
validates :name, :presence => true , :length => { :maximum => 50 }
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true, :format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:confirmation => true,
:length => {:within => 6..40}
before_save :encrypt_password
def feed
Micropost.from_users_followed_by_including_replies(self)
end
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def self.authenticate(email, submitted_password) # class method!
user = find_by_email(email)
# return nil if user.nil?
# return user if user.has_password?(submitted_password)
user && user.has_password?(submitted_password) ? user : nil
end
def self.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end
def following?(followed)
relationships.find_by_followed_id(followed)
end
def follow!(followed)
relationships.create!(:followed_id => followed.id)
end
def unfollow!(followed)
relationships.find_by_followed_id(followed).destroy
end
def shorthand
# name.gsub(/\s*/,"")
name.gsub(/ /,"_")
end
def self.shorthand_to_name(sh)
# name.gsub(/\s*/,"")
sh.gsub(/_/," ")
end
def self.find_by_shorthand(shorthand_name)
all = where(name: User.shorthand_to_name(shorthand_name))
return nil if all.empty?
all.first
end
private
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end
end
用户视图
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<%= render 'fields', :f => f %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
此处名称和电子邮件无法保存在db中,即使保存了密码并创建了新记录..