设计:无法找到用户ID的有效映射

时间:2015-11-18 10:17:18

标签: ruby-on-rails devise

部分名为locationpicker的以下表单:

<%= simple_form_for(current_user, url: registration_path(current_user),  method: :put ) do |f| %> 
   <% f.association :location, collection: Location.where(category: 'Country'), label: false, input_html: {onchange: "this.form.submit()"} %>
<% end %>

提出了这个错误:

Could not find a valid mapping for #<User id:...

导致它的registration_path(current_user)。我怀疑路线或用户模型中的某些东西已经发生变化,但我不能为我的生活思考什么或知道从哪里开始寻找。我认为它可能是最近包含的ActiveModel :: Dirty,但删除它并没有解决问题。

routes.rb中:

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do

  devise_for :users, :controllers => {:registrations => "registrations"}
  ...

end

User.rb

class User < ActiveRecord::Base


  include ActiveModel::Dirty

  devise :database_authenticatable, :registerable, #:encryptable,
     :recoverable, :rememberable, :trackable, :validatable, :lockable

  has_one :assignment
  has_one :role, :through => :assignment

  has_many :changerequests, inverse_of: :created_by, foreign_key: "created_by_id"
  belongs_to :location, required: true
  belongs_to :person, inverse_of: :user, required: true
  has_many :people_details, through: :person

  scope :inclusive, -> {includes(:person).includes(:people_details).includes(:location).includes(:assignment).includes(:role)}


  after_update :update_locale
  after_save :expire_caches


  def role?(role_sym)
    role_name.to_sym == role_sym
  end

  def role_group?(role_sym)
    role_group_name.to_sym == role_sym
  end



  def send_on_create_confirmation_instructions
    true
  end

  def update_locale
    if locale_changed?
      I18n.locale = self.locale || "en"
      self.expire_caches
    end
  end


  def country
   if self.location.category == "Country"
      self.location
    elsif self.location.ancestors.where(category: "Country")
      self.location.ancestors.where(category: "Country").first
    elsif self.location.children.where(category: "Country")
      self.location.children.where(category: "Country").first
    end
  end

  def expire_caches
    #Admin users can change their location so the caches need expiring
    if location_id_changed? or locale_changed?
      ctrlr = ActionController::Base.new
      #Clear fragments
      ctrlr.expire_fragment("#{id}_#{locale}_location_picker")
      etc...
    end
    if location_id_changed?
      #Clear other caches
      Rails.cache.delete("#{id}_locations_scope")
      .... etc

    end 
end

1 个答案:

答案 0 :(得分:0)

尝试在simple_form_for调用中添加as:

<%= simple_form_for(current_user, as: :user, url: registration_path(current_user),  method: :put ) do |f| %> 
   <% f.association :location, collection: Location.where(category: 'Country'), label: false, input_html: {onchange: "this.form.submit()"} %>
<% end %>

通常,设计注册编辑表单如下:

= simple_form_for(resource, as: resource_name, url: user_registration_path, html: { method: :patch }) do |f|

换句话说,使用&#34;资源&#34;不是&#34; current_user&#34;。如果您的表单部分在设计控制器上下文中呈现,您也可以尝试这样做。