添加嵌套属性以设计用户模型 [英] Adding nested attributes to devise user model

查看:63
本文介绍了添加嵌套属性以设计用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乍看之下,这个问题似乎已经被回答过了,但我的情况却没有(大多数与大规模分配问题和/或蒙古包有关).即使有很多问题,我也找不到答案.相关问题在这里.

At first glance it would seem this question has been answered before, but not on my case (most are related to mass assignment issues and/or mongoid.) I have not been able to find an answer even though there are plenty somewhat related questions here.

因此,基本上,我正在使用devise gem在ruby on rails应用程序上进行用户身份验证,并且我想向用户模型添加嵌套地址模型.我还使用了simple_form来生成表单视图.

So basically I'm using the devise gem for user authentication on a ruby on rails application and I want to add a nested address model to the user model. I'm also using simple_form to generate the form views.

我有一个用户模型:

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
  has_one :address

  accepts_nested_attributes_for :address, :allow_destroy => true
end

这是地址模型:

class Address < ActiveRecord::Base
  belongs_to :country
  belongs_to :state
  belongs_to :user

  attr_accessible :street1, :street2, :country_id, :state_id, :city, :postalCode
end

这是用户"控制器,为了添加显示操作以显示用户个人资料,我将其覆盖.

This is the Users controller, which I overrode in order to add a show action to display user profile.

class Users::RegistrationsController < Devise::RegistrationsController
  def new
    resource = build_resource({})
    resource.build_address
    respond_with resource
  end

  # POST /resource
  def create
    resource = build_resource(params[:user])

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

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

    #Add to view count if not viewing own user profile
    if @user != current_user
      @user.views += 1
      @user.save  
    end

    @vehicles = Vehicle.where(:user_id => @user)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end
end

这是用于创建新用户的视图:

This is the view for creating new users:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name) ) do |f| %>

              <fieldset>
                    <%= f.input :username, :required => false, :autofocus => true, placeholder: 'Pick a username' %>
                    <%= f.input :email, :required => false, placeholder: 'Your email' %>
                    <%= f.input :password, :required => false, placeholder: 'Create a password' %>
                    <%= f.input :password_confirmation, :required => false, placeholder: 'Confirm your password' %>
                    <%= f.association :roles, :as => :check_boxes, :label => "", :required => false %>
                    <%= f.simple_fields_for :address do |a| %>
                        <%= a.input :street1 %>
                        <%= a.input :street2 %>
                        <%= a.association :country %>
                        <%= a.association :state %>
                        <%= a.input :city %>
                        <%= a.input :postalCode %>
                    <% end %>
                    <%= f.button :submit, 'Sign up for free', :class => 'btn btn-success btn-large' %>
              </fieldset>
<% end %>

因此,创建新用户时的错误是: 地址用户不能为空

So the error upon creating a new user is this: Address user can't be blank

基本上没有为地址设置外键user_id.

Basically the foreign key user_id is not being set for address.

我希望有人可以对此问题有所了解.

I was hoping somebody can shed a little light on this issue.

推荐答案

忘记添加user_id来解决迁移问题.当向地址模型添加belongs_to:user时,Rails会期望它.

Forgot to add user_id to address migration. Rails expected it when adding belongs_to :user to the address model.

这篇关于添加嵌套属性以设计用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆