我使用Devise和rails 4进行用户身份验证。我之前已经自定义了注册/编辑表单,并根据需要添加了字段,没有任何问题。通过我的最新修改,我在表格中添加了两个字段(类别和网站)。它们显示在db / schema中,当我保存表单时它们会被传递,但值没有被保存。
我的表格:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
....preceding fields....
<div class="form-field-area">
<p class="form-label"><%= f.label :category_name %></p>
<p class="form-label"><%= f.collection_select :category_name, Category.order(:name), :id, :name, :include_blank => true %></p>
</div>
<div class="form-field-area">
<p class="form-label"><%= f.label :website %></p>
<p class="form-label"><%= f.text_field :website, :value => current_user.website %></p>
</div>
<% end %>
我也有一个用户控制器,有一个更新方法,允许我使用rolify从网站分配角色。我无法记住,从那时起我将设置保存到设计表的麻烦才开始,但无论如何都是用户控制器:
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
@user.add_role params[:user][:role]
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
if @user.update(user_params)
redirect_to user_index_path, notice: "Update Successful"
else
render :edit
end
end
我的部分路线,以防万一:
devise_for :users, controllers: {:registrations => "registrations"}
resources :users do
resources :posts
resources :adverts
end
就像我说的,我遇到的问题是我的设计表格中的类别和网站字段没有保存,我不知道是否因为我已经添加了这种自定义更新方法是否与用户控制器有关。没有错误被抛出。
修改
这里打印出日志中的整个操作
Started PUT "/users" for 192.168.0.13 at 2014-09-09 16:38:51 +0100
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sJHiKHOtWBQkQHIWopu1gk4ZDyW/WaknbcAya3cN8iM=", "user"=>{"full_name"=>"Sheeka Patak", "email"=>"sheeka.patak@gmail.com", "password"=>"[FILTE
RED]", "password_confirmation"=>"[FILTERED]", "business_name"=>"Fake Company", "trading_name"=>"", "category_name"=>"7", "phone"=>"01 666-7777", "website"=>"www.fakeaddress.com", "street_lin
e_one"=>"", "street_line_two"=>"", "town"=>"", "about"=>"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. N
ulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent tacit
i sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor.", "opening_times"=>"Mon - Fri: 9 -
5\r\nSat : 10 - 3\r\nSun : Closed", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
User Load (5.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 47 ORDER BY "users"."id" ASC LIMIT 1
User Load (4.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 47]]
Unpermitted parameters: trading_name, category_name, website
(0.2ms) begin transaction
(0.2ms) commit transaction
Redirected to http://192.168.0.20:3000/
Completed 302 Found in 830ms (ActiveRecord: 10.8ms)
答案 0 :(得分:2)
如果查看日志,就会说:
未经许可的参数:trading_name,category_name,website
您需要允许自己的属性。如果您查看devise docs
, 以更新帐户,则需要在devise_paramter_sanitizer 中使用:account_update
。在application_controller.rb中添加它
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:full_name, :email, :password, :business_name, :trading_name, :category_name, :phone, :website, :street_line_one, :street_line_two, :town, :about, :opening_times, ) }
end
end