我将字段user_name和job_function添加到使用Devise创建的用户模型中。我可以将字段添加到视图中,但这两个新字段不会保存到数据库中。仅限:保存电子邮件和:密码,这是已经发生的事情。
***** UPDATE:
我收到以下错误: 未允许的参数:user_name,job_function
所以我正在考虑绕过那个。任何想法都表示赞赏。
提前感谢。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
:configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:user_name,
:job_function)}
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:user_name,
:job_function)}
devise_parameter_sanitizer.for(:update) { |u| u.permit(:user_name,
:job_function)}
end
end
views / devise / registrations / new.html
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :user_name %><br />
<%= f.text_field :user_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :job_function %><br />
<%= f.radio_button :job_function, 'EMT' %> EMT
<%= f.radio_button :job_function, 'Paramedic' %> Paramedic
<%= f.radio_button :job_function, 'Fire' %> Fire
<%= f.radio_button :job_function, 'Nursing' %> Nursing
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
模式
ActiveRecord::Schema.define(version: 20170429200947) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "articles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "location_id"
t.string "title"
t.string "body"
t.index ["location_id"], name: "index_articles_on_location_id", using: :btree
t.index ["user_id"], name: "index_articles_on_user_id", using: :btree
end
create_table "locations", force: :cascade do |t|
t.string "location_name"
t.string "location_address"
t.string "location_description"
t.float "lat"
t.float "lng"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "user_name"
t.string "job_function"
t.boolean "admin"
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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "provider"
t.string "uid"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
add_foreign_key "articles", "locations"
add_foreign_key "articles", "users"
end
路线
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
root 'homes#index'
get '/locations', to: 'locations#index', as: 'locations'
get '/locations/new', to: 'locations#new', as: 'new_location'
get '/locations/splash', to: 'locations#splash', as: 'splash'
# Location Routes
get '/locations/map', to: 'locations#map', as: 'map'
post '/locations', to: 'locations#create'
get '/locations/:id', to: 'locations#show', as: 'location'
get '/locations/:id/edit', to: 'locations#edit', as: 'edit_location'
patch '/locations/:id', to: 'locations#update'
delete '/locations/:id', to: 'locations#destroy'
# Article Routes
get '/articles/:id/new', to: 'articles#new', as: 'new_article'
get '/articles/:id', to: 'articles#show', as: 'article'
get '/articles', to: 'articles#index', as: 'user_articles'
post '/articles', to: 'articles#create'
delete '/articles/:id', to: 'articles#destroy', as:'delete_article'
get '/articles/:id/edit', to: 'articles#edit', as: 'edit_article'
put '/articles/:id', to: 'articles#update'
patch '/articles/:id', to: 'articles#update'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy'
end
答案 0 :(得分:0)
想出来。我需要将以下内容添加到我的ApplicationController:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:user_name, :job_function])
end