这是我的注册码:
<%= render 'shared/errors',obj:@user %>
<div class="row">
<div class="col-xs-12">
<%= form_for(@user, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :username %>
</div>
<div class="col-sm-8">
<%= f.text_field :username, class: "form-control", placeholder: "Enter username", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :email %>
</div>
<div class="col-sm-8 ">
<%= f.email_field :email, class: "form-control", placeholder: "Enter email", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :password %>
</div>
<div class="col-sm-8">
<%= f.password_field :password,class: "form-control",placeholder: "Enter password" %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :password_confirmation, 'Confirm' %>
</div>
<div class="col-sm-8">
<%= f.password_field :password_confirmation,class: "form-control", autofocus:true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :phone %>
</div>
<div class="col-sm-8 ">
<%= f.text_field :phone, class: "form-control", placeholder: "Phone number", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :city %>
</div>
<div class="col-sm-8 ">
<%= f.text_field :city, class: "form-control", placeholder: "Enter city", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :birthday %>
</div>
<div class="col-sm-8 ">
<%= f.date_field :birthday, class: "form-control", placeholder: "Enter birthday", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="col-sm-1 col-xs-offset-2">
<%= f.radio_button :sex, "female" %>
</div>
<div class="control-label col-sm-1">
<%= f.label :sex, "Female", autofocus: true %>
</div>
<div class="col-sm-1">
<%= f.radio_button :sex, "male" %>
</div>
<div class="control-label col-sm-1">
<%= f.label :sex, "Male", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit(@user.new_record? ? "Sign up" : "Update account", class: "btn btn-primary btn-lg")%>
</div>
</div>
<% end %>
<div class="col-xs-4 col-xs-offset-4">
[<%= link_to "Cancel request and return to articles listing", articles_path %>]
</div>
</div>
</div>
这是我的迁移代码:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :phone
t.string :city
t.date :birthday
t.string :sex
t.timestamps
end
end
end
这是我的用户显示代码:
<br><h3 align="center">Welcome to <%= @user.username %>'s page</h3>
<div class="row">
<div class="col-md-4 col-md-offset-4" align="center">
<%= gravatar_for @user, size: 150 %>
</div>
</div>
<br><div class="row align="center" ">
Sex: <%= @user.sex %><br>
Phone: <%= @user.phone %><br>
City: <%= @user.city %><br>
Birthday: <%= @user.birthday %>
<h4><%= @user.username %>'s articles</h4>
</div>
<div align="center">
<%= will_paginate @user_articles %>
</div>
<%= render 'articles/article', obj: @user_articles %>
<div align="center">
<%= will_paginate @user_articles %>
</div>
但sex
,phone
,city
和birthday
未显示在用户展示页面中。如何将此数据保存到数据库以便显示?
这是我的用户控制器:
class UsersController < ApplicationController
before_action :set_user, only: [:edit, :update, :show]
before_action :require_same_user, only: [:edit, :update, :destroy]
before_action :require_admin, only: [:destroy]
def index
@users = User.paginate(page: params[:page], per_page: 5)
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
flash[:success] = "Welcome to Loccaf #{@user.username}"
redirect_to user_path(@user)
else
render 'new'
end
end
def edit
end
def update
if @user.update(user_params)
flash[:success] = "Your account was updated successfully"
redirect_to articles_path
else
render 'edit'
end
end
def show
@user_articles = @user.articles.paginate(page: params[:page], per_page: 5)
end
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:danger] = "User and all articles created by user have been deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:username,:email,:password)
end
def set_user
@user = User.find(params[:id])
end
def require_same_user
if current_user != @user and !current_user.admin?
flash[:danger] = "You can only edit your own articles"
redirect_to root_path
end
end
def require_admin
if logged_in? and !current_user.admin?
flash[:danger] = "Only admin users can perform this action"
redirect_to root_path
end
end
end
这是我的用户模型:
class User < ActiveRecord::Base
has_many :articles, dependent: :destroy
before_save {self.email = email.downcase}
validates :username, presence: true,
uniqueness: {case_sensitive: false},
length: {minimum:3, maximum: 25}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+\.[a-z]+\z/i
validates :email, presence: true,length: {maximum: 105},
uniqueness: {case_sensitive: false},
format: {with: VALID_EMAIL_REGEX}
has_secure_password
end
答案 0 :(得分:1)
您没有在user_params
方法中将这些属性列入白名单。以下代码应该可以使用
def user_params
params.require(:user).permit(:username, :email, :password, :sex, :phone, :city, :birthday)
end