Ruby on rails 嵌套设计模型形式
我正在使用Desive对用户进行身份验证,我希望在以相同的形式注册时为用户分配一个位置。我当前在提交表单时遇到此错误。有什么想法吗 ActiveRecord::DeviceInvitable::RegistrationController#create中的AssociationTypeMismatch 位置(#70224765161800)应为,已获得ActiveSupport::HashWithInferenceAccess(#70224763369340) 我的位置模型Ruby on rails 嵌套设计模型形式,ruby-on-rails,devise,nested-forms,Ruby On Rails,Devise,Nested Forms,我正在使用Desive对用户进行身份验证,我希望在以相同的形式注册时为用户分配一个位置。我当前在提交表单时遇到此错误。有什么想法吗 ActiveRecord::DeviceInvitable::RegistrationController#create中的AssociationTypeMismatch 位置(#70224765161800)应为,已获得ActiveSupport::HashWithInferenceAccess(#70224763369340) 我的位置模型 class Loca
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
用户/控制器
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
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
class UsersController“未被授权为管理员。”
@users=User.all
结束
def秀
@user=user.find(参数[:id])
结束
def更新
授权!:更新,@user,:message=>“未被授权为管理员。”
@user=user.find(参数[:id])
如果@user.update_属性(参数[:user],:as=>:admin)
将\重定向到用户\路径,:notice=>“用户已更新。”
其他的
将\重定向到用户\路径:警报=>“无法更新用户。”
结束
结束
def销毁
授权!:销毁,@user,:message=>“未被授权为管理员。”
user=user.find(参数[:id])
除非用户==当前用户
用户破坏
将\重定向到用户\路径:notice=>“用户已删除。”
其他的
重定向到用户路径:注意=>“无法删除您自己。”
结束
结束
结束
您正在创建一个用户,因此User类应该具有accepts\u nested\u attributes\u for:location。但是我不确定它是否能解决问题…您正在创建一个用户
,因此用户
类应该具有接受嵌套的属性\u for:location
。但我不确定它是否能解决问题……谢谢@Baldrick,就是这样。
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
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