添加嵌套属性来设计用户模型

2023-12-01

乍一看,这个问题似乎以前已经得到解答,但在我的情况下却没有得到解答(大多数与批量分配问题和/或 mongoid 有关。)尽管这里有很多相关的问题,但我一直无法找到答案。

所以基本上我在 ruby​​ on Rails 应用程序上使用 devise gem 进行用户身份验证,并且我想向用户模型添加嵌套地址模型。我还使用 simple_form 生成表单视图。

我有一个用户模型:

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

这是用户控制器,我重写了它以添加显示操作来显示用户个人资料。

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

这是创建新用户的视图:

<%= 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 %>

所以创建新用户时的错误是这样的: 地址用户不能为空

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

我希望有人能够对这个问题有所启发。


我能够在我的应用程序中解决这个问题,但我使用了强大的参数。但是该解决方案应该仍然适用于您的情况。

而不是重写中的所有方法RegistrationsController我刚刚超越了build_resource传入所需的正确参数。

def build_resource(params)
  super(resource_params)
end

def resource_params
  params.require(:user).permit(:email, :password, tickets_attributes: [ :description ] )
end

我也有inverse_of on my User and Ticket模型,像这样:

user.rb
  has_many :tickets, inverse_of: :user

ticket.rb
  belongs_to :user, inverse_of :tickets
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

添加嵌套属性来设计用户模型 的相关文章

随机推荐