1

好的,我进行了广泛的搜索,找不到可以开始工作的解决方案……所以我决定在这里发帖。

我有 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>
4

2 回答 2

4

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

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

  # etc
end

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

  if @user.save
    @user.create_store
    # etc
  end
于 2012-05-13T20:49:07.437 回答
1

你可能想看看这个:

http://railscasts.com/episodes/196-nested-model-form-part-1

于 2012-05-13T21:54:52.633 回答