2015-11-18 27 views
0

在部分稱爲locationpicker以下形式的有效映射:設計:找不到用戶ID

<%= 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 

回答

0

嘗試增加爲:在您的通話simple_form_for:

<%= 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| 

換句話說,使用「資源」而不是「current_user」。如果您的表單部分在設計控制器上下文中呈現,您也可以嘗試一下。

+0

是的,這已經得到它了:'<%= simple_form_for(current_user,as :: user,url:user_registration_path,method :: put)do | f | %>'。但我想知道爲什麼它在很多年前都有效? – thenapking