我正在使用设计对用户进行身份验证,并且我想在注册时以相同的形式为用户分配一个位置。我目前在提交表单时收到此错误。有任何想法吗?
DeviseInvitable::RegistrationsController#create 中的 ActiveRecord::AssociationTypeMismatch
预期位置(#70224765161800),获得 ActiveSupport::HashWithIndiffergentAccess(#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 %>
位置控制器
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