Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to insert same data into two tables using nested forms
    primarykey
    data
    text
    <p>I am using nested forms to insert data into two tables (user and address).I want to have my users email id in both the table, but the user should enter the email id once. Here is my current view</p> <pre><code>&lt;%= form_for @user do |f| %&gt; &lt;%= f.text_field(:name, :size =&gt; 20) %&gt; &lt;%= f.text_field(:email, :size =&gt; 20) %&gt; &lt;%= f.fields_for :address do |r| %&gt; &lt;%= r.text_field(:street, :size =&gt; 20) %&gt; &lt;%= r.text_field(:city, :size =&gt; 20) %&gt; &lt;% end %&gt; &lt;%= f.submit "Create" %&gt; &lt;% end %&gt; </code></pre> <p>In my nested section "address" i once again want the email field, but i don't want a repeated text field for email. How can i use the email section inside the nested form "address"? I have tried using hidden form fields, but it didn't worked. Can somebody help please. </p> <p>EDIT</p> <p>Controller(Only question related parts)</p> <pre><code>class UsersController &lt; ApplicationController def new @domain = Domain.find(params[:id]) @user = @domain.users.build @title = "Add User" end def create @user = User.new(params[:user]) if @user.save flash[:success] = "Welcome!" redirect_to manageDomain2_path(:id =&gt; @user.domain_id) else @title = "Add User" redirect_to addNewUser_path(:id =&gt; @user.domain_id) end end </code></pre> <p>I have tried with:</p> <pre><code>@user = User.new(params[:user][:address_attributes].merge!(:email =&gt; params[:user][:email])) </code></pre> <p>When using:</p> <pre><code>@user = User.new(params[:user][:address].merge!(:email =&gt; params[:user][:email])) </code></pre> <p>I am getting error "Undefined method merge".</p> <p>User Model (Question related portion only)</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible: :email, :domain_id, :address_attributes belongs_to :domain has_one :address accepts_nested_attributes_for :address end </code></pre> <p>Address Model (Full)</p> <pre><code>class Address &lt; ActiveRecord::Base attr_accessible :user_id, :email, :street, :city end </code></pre> <p>When i use </p> <pre><code>belongs_to :user </code></pre> <p>in address model i get syntax error. So i have tried without using it. the user_id, street, city is getting proper values but email email field is not getting any thing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload