同时创建2个模型

时间:2012-05-13 20:42:56

标签: ruby-on-rails forms model

好的我搜索得很远,无法找到可以解决的解决方案......所以我决定在这里发帖。

我有2个模特

商品

class Store < ActiveRecord::Base
  attr_accessible :storeimage, :storename
  belongs_to :user

  validates :user_id, :presence => true  
end

用户

class User < ActiveRecord::Base 
  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :userimage, :remove_userimage
  has_secure_password
  has_many :gears
  has_many :comments, :dependent => :destroy 
  has_one :store, :dependent => :destroy
  before_save :create_remember_token
  require 'carrierwave/orm/activerecord'
  mount_uploader :userimage, UserpicUploader
  accepts_nested_attributes_for :store

  ...

end

当有人创建新用户帐户时,我需要自动为用户表单中的用户创建新商店。那么如何创建一个链接到正在创建的新用户的新商店对象呢?

以下是来自 CreateAction用户控制器

的代码
  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      redirect_to @user, :flash => {:success => "Welcome to Equiptme"}
    else
      render 'new'
      @title = "Sign up"
    end
  end

查看

<div class="signup_container">
    <div class="signup_container_interior">
    <%= provide(:title, 'Sign up') %>
    <%= form_for(@user) do |f| %>
    <% if @user.errors.any? %>
      <div>
        <div>
          The form contains <%= pluralize(@user.errors.count, "error") %>.
        </div>
        <ul>
        <% @user.errors.full_messages.each do |msg| %>
          <li>* <%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>
      <div class="register_field">  
        <div class="register_nonerror_container">
            <%= f.label :first_name %>&nbsp;<%= f.text_field :first_name, class: 'register_text_area' %>
        </div>
      </div>
      <div class="register_field">
        <div class="register_nonerror_container">
            <%= f.label :last_name %>&nbsp;<%= f.text_field :last_name, class: 'register_text_area' %>
        </div>
      </div>
      <div class="register_field">
        <div class="register_nonerror_container">
            <%= f.label :email %>&nbsp;<%= f.text_field :email, class: 'register_text_area' %>
        </div>
      </div>
    <!--************STORE FIELDS ************** -->

    <!--************STORE FIELDS END ************** --> 

      <div class="register_field">
        <div class="register_nonerror_container">
            <%= f.label :password %>&nbsp;<%= f.password_field :password, class: 'register_text_area' %>
        </div>
      </div>
      <div class="register_field">
        <div class="register_nonerror_container">
            <%= f.label :password_confirmation %>&nbsp;<%= f.password_field :password_confirmation, class: 'register_text_area' %>
        </div>
      </div>
      <div class="actions">
        <%= f.submit "Create Account", class: 'register_button' %>
      </div>
    <% end %>
    </div>
</div>

2 个答案:

答案 0 :(得分:4)

您可以使用build_association method创建以及用户和商店之间的has_one关系:

def create
  @user = User.new(params[:user])
  @user.build_store

  # etc
end

如果您在保存用户之前不需要商店,则可以使用create_association

  if @user.save
    @user.create_store
    # etc
  end

答案 1 :(得分:1)