2013-01-01 19 views
0

我使用設計來驗證用戶,我想在註冊時以相同的形式爲用戶指定位置。提交表單時,我目前收到此錯誤。有任何想法嗎?嵌套設計模型表格

的ActiveRecord :: AssociationTypeMismatch在DeviseInvitable :: RegistrationsController#創建

位置(#70224765161800)預計,得到的ActiveSupport :: HashWithIndifferentAccess(#70224763369340)

我的位置模型

class Location < ActiveRecord::Base 
has_many :users, :dependent => :destroy 
accepts_nested_attributes_for :users, :allow_destroy => true 
attr_accessible :lat, :long, :name, :street_adress 
attr_accessible :user_attributes 
end 

我用戶型號

class User < ActiveRecord::Base 
belongs_to :location 
rolify 
devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
attr_accessible :role_ids, :as => :admin 
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :location 
end 

我new.html.erb視圖

<% resource.build_location %> 
<%= simple_form_for(resource, :as => resource_name, :url =>  registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %> 
<%= f.error_notification %> 

<%= f.fields_for @location do |location_form| %> 
<%= location_form.text_field :name %> 
<% end %> 

<%= f.input :name, :autofocus => true %> 
<%= f.input :email, :required => true %> 
<%= f.input :password, :required => true %> 
<%= f.input :password_confirmation, :required => true %> 

<%= f.button :submit, 'Sign up', :class => 'btn-primary' %> 
<% end %> 

locations_controller

def new 
    @location = Location.new 
    @location.user.build 
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @location } 
    end 
    end 

    # GET /locations/1/edit 
    def edit 
    @location = Location.find(params[:id]) 
    end 

    # POST /locations 
    # POST /locations.json 
    def create 
    @location = Location.new(params[:location]) 

    respond_to do |format| 
     if @location.save 
     format.html { redirect_to @location, notice: 'Location was successfully created.' } 
     format.json { render json: @location, status: :created, location: @location } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @location.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

users_controller

class UsersController < ApplicationController 
    before_filter :authenticate_user! 

    def index 
    authorize! :index, @user, :message => 'Not authorized as an administrator.' 
    @users = User.all 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 

    def update 
    authorize! :update, @user, :message => 'Not authorized as an administrator.' 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user], :as => :admin) 
    redirect_to users_path, :notice => "User updated." 
    else 
    redirect_to users_path, :alert => "Unable to update user." 
    end 
    end 

    def destroy 
    authorize! :destroy, @user, :message => 'Not authorized as an administrator.' 
    user = User.find(params[:id]) 
    unless user == current_user 
     user.destroy 
     redirect_to users_path, :notice => "User deleted." 
    else 
     redirect_to users_path, :notice => "Can't delete yourself." 
    end 
    end 
end 
+2

你正在創建一個'User',所以'User'類應該有'accep_nested_attributes_for:location'。但我不知道它會解決問題... – Baldrick

+1

謝謝@Baldrick就是這樣。 – jacobt

回答

0

你正在創建一個用戶,因此用戶類應該有accepts_nested_attributes_for:位置。但我不知道它會解決問題...