Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嵌套设计表单不链接模型_Javascript_Ruby On Rails_Devise_Nested Forms_Google Places Api - Fatal编程技术网

Javascript 嵌套设计表单不链接模型

Javascript 嵌套设计表单不链接模型,javascript,ruby-on-rails,devise,nested-forms,google-places-api,Javascript,Ruby On Rails,Devise,Nested Forms,Google Places Api,我正在尝试创建一个新的位置,并设计一个相同形式的用户,并将它们链接起来。用户和位置已创建,但位置id未保存给用户。用户表中有一个location\u id列 我的表格 <% resource.build_location %> <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => '

我正在尝试创建一个新的位置,并设计一个相同形式的用户,并将它们链接起来。用户和位置已创建,但位置id未保存给用户。用户表中有一个location\u id列

我的表格

<% resource.build_location %>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %>
 <%= f.error_notification %>
 <!-- capture location details hidden values -->
  <%= f.fields_for :location do |location_form| %>
  <%= location_form.text_field :name, :name => "name", :type => "hidden" %>
  <%= location_form.text_field :street_address, :name => "formatted_address", :type => "hidden" %>
  <%= location_form.text_field :lat, :name => "lat", :type => "hidden" %>
  <%= location_form.text_field :long, :name => "lng", :type => "hidden" %>
<% end %>
 <!-- devise user authenticate --> 
  <%= 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 %>
<%= render "devise/shared/links" %>
用户模型

class User < ActiveRecord::Base
  belongs_to :location
  accepts_nested_attributes_for :location
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :role_ids, :as => :admin
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :location, :location_id, :location_attributes
end
class用户:admin
属性可访问:名称、电子邮件、密码、密码确认、记住我、位置、位置id、位置属性
结束
用户控制器

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 = @user.location.build(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=>“用户已删除。”
其他的
重定向到用户路径:注意=>“无法删除您自己。”
结束
结束
结束
创建用户时没有错误,仅在创建用户时获取位置。我可以访问该位置,并创建了一个位置id,但未链接到用户。关于如何将位置id保存给用户有什么想法吗


我正在使用从google location api自动完成返回的json填充位置信息,并将其分配给名为“”的元素。当我手动输入位置信息时,似乎一切正常,但当从自动完成填充字段时,一切都失败了。

首先要注意的是,在这两个模型上,您不需要
接受
的嵌套属性,只有具有
的模型才有多个
关联。此外,在用户属性的位置模型中,多元化似乎是错误的

class Location < ActiveRecord::Base
  has_many :users, :dependent => :destroy
  accepts_nested_attributes_for :users, :allow_destroy => true
  attr_accessible :lat, :long, :name, :street_address
  attr_accessible :users_attributes # <- This should be plural
end
还有这条线

@location = @user.location.build(params[:location])
应该是

@location = Location.new(params[:location)
由于现在模型的设置方式是一个位置有一个用户,因此您不需要根据用户构建位置。也就是说,我建议您在用户有许多位置的相反方向创建关联,但这当然可能与您的功能相反,所以请恕我直言:)


谢谢Jason,问题是如果我删除用户模型中的accepts_nested_attributes_,我会得到以下错误:ActiveRecord::DesiveInvitable::RegistrationsController中的AssociationTypeMismatch#创建位置(#70224765161800)预期,获得ActiveSupport::HashWithInferenceAccess(#70224763369340)使用
用户控制器可能有问题。因为您使用的是Desive,所以实际上您根本不需要它,如果您想覆盖Desive提供的功能,您必须覆盖其控制器本身。请参阅此以获取参考:。如果没有,您可能还希望从用户模型中删除
:location
location\u属性。谢谢Jason,我来看看。我确信有更好的方法可以做到这一点,尽管我仍然有点困惑,为什么当我手动将数据输入到text_字段时它会起作用,而当同一text_字段接收到json文本时它就不起作用了。
@location = @user.location.build(params[:location])
@location = Location.new(params[:location)