一切似乎都很好,执行user.save时没有错误消息,但在索引时不显示。Ruby rails无法添加用户条目
我跑迁移:
class AddEmailToUsers < ActiveRecord::Migration
def change
add_column :email, :name, :password, :password_confirmation, :string
end
end
型号/ 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+\-.][email protected][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
这里上创建我填写一些样本值进行测试,并将其提交到的意见/用户/创建.html.erb没有任何内容显示在“错误:[]”中,并且显示了@user字段显示的正确数据。
控制器/ 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:"[email protected]", 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
的意见/用户/ 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 %>
但这用户索引不显示任何用户都:
意见/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>
[编辑]
我跑迁移(marouenB),但现在当我索引用户来说,它说:
ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
运行不更改消息。
运行耙分贝:迁移:状态显示:
Status Migration ID Migration Name
--------------------------------------------------
up 20150328013052 ********** NO FILE **********
up 20150330041328 Create model forums
up 20150331165353 Add email to users
up 20150404003549 Add password digest to users
down 20150506210153 Add login to users
[编辑]
我不知道我做出DB /迁移是永久性的任何变化。所以我通过重新创建具有相同日期戳记的旧迁移文件来修复它。然后,我必须删除您的迁移中的更改内容,因为它已经创建了这些字段。我必须
drop_table :users
因为它给所有这些零字段的错误。现在我有一个新的问题,但这个问题解决了。
您确定迁移有效吗?另外,什么显示日志? – Aguardientico
你没有告诉Rails应该在哪个表中添加列。 – spickermann