【问题标题】:Rails 4/Devise: Attribute not savingRails 4/Devise:属性不保存
【发布时间】:2014-09-10 00:14:18
【问题描述】:

我正在使用带有 rails 4 的 Devise 进行用户身份验证。我之前已经自定义了注册/编辑表单,并根据需要添加了字段,没有任何问题。通过我的最新修改,我在表格中添加了另外两个字段(类别和网站)。它们出现在 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)

【问题讨论】:

  • 日志中显示了什么?什么控制器/动作正在处理请求?它接收什么参数?发生了哪些数据库查询?
  • @Jordan 我已经添加了日志中的打印输出对问题的说明。

标签: ruby-on-rails devise


【解决方案1】:

如果你查看你的日志,它会说:

不允许的参数:trading_name、category_name、网站

您需要允许您的属性。如果您查看 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

【讨论】:

  • 你知道吗,我只是再次查看了允许的参数并意识到我有一个错字。我现在觉得很愚蠢,但这基本上是问题所在,所以我会接受你的回答:)
  • @SoSimple 您没有粘贴您的许可方法,并且从您的日志中某些属性是不允许的,所以认为您忘记添加它们了:)
  • 是的,这是我的疏忽,我没有正确阅读日志。无论如何感谢您的帮助,如果您不提及它,我就不会检查!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多