[当用户注册时,我在设计中添加了一个额外的字段:“ barge_name”,当我进入注册页面时,我输入了一个barge_name,电子邮件地址和密码,所有这些都可以使用。
但是当我尝试使用rails c时,User.first是说barge_name为nil,我不知道为什么注册时未获取该值?有人可以帮我吗?
#<User id: 1, email: "[email protected]", created_at: "2019-10-31 17:43:21", updated_at: "2019-10-31 17:43:21", barge_name: nil>]
模式
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "barge_name"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
迁移文件
class AddDetailsToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :barge_name, :string
end
end
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
end
end
表格
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :barge_name,
required: true,
autofocus: true,
input_html: { autocomplete: "bargename" }%>
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.input :password,
required: true,
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
控制器
class PositionsController < ApplicationController
def new
@position = Position.new
end
def index
@positions = Position.all
end
def show
@position = Position.find(params[:id])
end
def create
@position = Position.new(position_params)
if @position.save!
redirect_to @position
PositionMailer.general_message(@position).deliver
else
render :new
end
end
private def position_params
params.require(:position).permit(:date, :time, :activity, :tripnumber)
end
end
您不允许barge_name
参数,请尝试:
class PositionsController < ApplicationController
def new
@position = Position.new
end
def index
@positions = Position.all
end
def show
@position = Position.find(params[:id])
end
def create
@position = Position.new(position_params)
if @position.save!
redirect_to @position
PositionMailer.general_message(@position).deliver
else
render :new
end
end
private
def position_params
params.require(:position).permit(:date, :time, :activity, :tripnumber, :barge_name)
end
end