English 中文(简体)
同时创建2个模型
原标题:Create 2 Models at the same time

奥基斯·伊夫进行了广泛深入的探索,找到了我可以找到的解决办法。

我有2个模式

http://www.ohchr.org。

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

  validates :user_id, :presence => true  
end

以及

www.un.org/spanish/ecosoc 用户<>

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

当有人创建新的用户账户时,我需要自动为该用户建立一个新的仓库,我是在用户表中思考的。 因此,我如何能够创建与新用户连接的新储存物体?

下面是我从发出的守则。 创建行动用户主计长

  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>
最佳回答

您可使用build_associationmeth。 与<代码>一起创建的用户与仓库之间的关系:

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

  # etc
end

如果在您拯救用户之前不需要该仓库,你也可以使用<代码>create_association:

  if @user.save
    @user.create_store
    # etc
  end
问题回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签