1

乍一看,这个问题似乎已经被回答过,但在我的案例中却没有(大多数都与批量分配问题和/或 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。

我希望有人可以对这个问题有所了解。

4

2 回答 2

0

忘记添加 user_id 来解决迁移问题。Rails 在将 belongs_to :user 添加到地址模型时期望它。

于 2013-10-30T23:06:39.477 回答
0

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

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我的UserTicket模型,像这样:

user.rb
  has_many :tickets, inverse_of: :user

ticket.rb
  belongs_to :user, inverse_of :tickets
于 2013-09-05T14:11:11.240 回答