嵌套设计模型表单

时间:2013-01-01 17:25:50

标签: ruby-on-rails devise nested-forms

我正在使用设计来验证用户,并且我希望在注册时以相同的形式为用户分配位置。我目前在提交表单时收到此错误。有什么想法吗?

DeviseInvitable中的ActiveRecord :: AssociationTypeMismatch :: RegistrationsController #create

预计位置(#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

1 个答案:

答案 0 :(得分:0)

您正在创建用户,因此User类应具有accepts_nested_attributes_for:location。但我不确定它会解决这个问题......