I am trying to create a form that updates 2 tables in my database. I'm not
quite sure how to build this contact form quite yet. So my first short term
goal is to simply update my `contact_requests` and `users` tables. I am
aiming to do a `one to many` relationship between `users` and
`contact_requests`. This allows one `user` to have many `contact_requests`.
I am new to rails so I feel like there are gaps in my code and I am making
some mistakes as well. Ideally, I will want to include a mailer in the
form as well but one step at a time.

I currently have a `user` table and a `contact_request` table:

<https://lh5.googleusercontent.com/-qZtDF_xGMys/VHpT5tg4SMI/AAAAAAAAAGM/WB4j3-K3p08/s1600/Screen%2BShot%2B2014-11-29%2Bat%2B2.48.18%2BPM.png>


`user_id` from the `contact_requests` table is a foreign key that
references `users.id`

Here is by current schema code:

     ActiveRecord::Schema.define(version: 20141127114323) do
       create_table "contact_requests", force: true do |t|
         t.datetime "created_at", null: false
         t.datetime "updated_at", null: false
         t.string "message", null: false
         t.integer "user_id"
       end

       add_index "contact_requests", ["user_id"], name:
"index_contact_requests_on_user_id", using: :btree

       create_table "users", force: true do |t|
         t.datetime "created_at", null: false
         t.datetime "updated_at", null: false
         t.string "first_name", null: false
         t.string "last_name", null: false
         t.string "email", null: false
         t.string "phone_number"
       end
     end

Here are my models and controllers:

     class User < ActiveRecord::Base
       has_many :contact_requests
       # validate the presence of the attributes
       validates(:first_name, presence: true)
       validates(:last_name, presence: true)
       validates(:email, presence: true)
     end

     class ContactRequest < ActiveRecord::Base
       belongs_to :user
       validates :user_id, presence: true
       validates :message, presence: true, length: { maximum: 500 }
     end

     class ContactController < ApplicationController
       def new
       @user = User.new
       @contact_request = ContactRequest.new
       end
     end


I am getting this error:

<https://lh4.googleusercontent.com/-MRCqb9NOm6A/VHpTtDohT-I/AAAAAAAAAGE/hqgxKaH07bE/s1600/Screen%2BShot%2B2014-11-29%2Bat%2B2.34.29%2BPM.png>



form:

     <%= form_for(:contact, remote: true, class: "contact-input") do |f| %>
           <div class="col-md-12">
             <div class="col-md-6">
               <div class="contact-input-margin form-group">
                 <%= f.text_field(:user, :first_name, class: "form-control",
placeholder: "First name")%>
               </div>
               <div class="contact-input-margin form-group">
                 <%= f.text_field(:user, :last_name, class: "form-control",
placeholder: "Last name") %>
               </div>
               <div class="contact-input-margin form-group">
                 <%= f.email_field(:user, :email, class: "form-control",
placeholder: "Email") %>
               </div>
               <div class="contact-input-margin form-group">
                 <%= f.telephone_field(:user, :phone_number, class:
"form-control", placeholder: "Phone number") %>
               </div>
             </div>
             <div class="contact-input-margin col-md-6">
               <div class="form-group">
                 <%= f.text_area(:contact_request, :message, class:
"form-control contact-margin", rows: "8", placeholder: "Message...") %>
               </div>
             </div>
           </div>
           <%= f.submit(class: "btn btn-xl") %>
         <% end %>


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6f73f2d0-e80d-4ff1-89a0-1e1b63ffa3ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jason Fleetwood-Boldt at Nov 30, 2014 at 2:50 am
    You need to add accepts_nested_attributes_for :user to your ContractRequest model and then your style of referencing those fields in your form changes to something like


      <%= f.text_field("user[last_name]", class: "form-control", placeholder: "Last name") %>


    I think that should do the trick but read up on accepts_nested_attributes_for cause it's a little tricky to get it right.


    come to think of it on 2nd thought maybe it's

      <%= f.text_field("user_attributes[last_name]", class: "form-control", placeholder: "Last name") %>


    -Jason

    On Nov 29, 2014, at 6:21 PM, bradford li wrote:



    I am trying to create a form that updates 2 tables in my database. I'm not quite sure how to build this contact form quite yet. So my first short term goal is to simply update my `contact_requests` and `users` tables. I am aiming to do a `one to many` relationship between `users` and `contact_requests`. This allows one `user` to have many `contact_requests`. I am new to rails so I feel like there are gaps in my code and I am making some mistakes as well. Ideally, I will want to include a mailer in the form as well but one step at a time.

    I currently have a `user` table and a `contact_request` table:

    <https://lh5.googleusercontent.com/-qZtDF_xGMys/VHpT5tg4SMI/AAAAAAAAAGM/WB4j3-K3p08/s1600/Screen%2BShot%2B2014-11-29%2Bat%2B2.48.18%2BPM.png>


    `user_id` from the `contact_requests` table is a foreign key that references `users.id`

    Here is by current schema code:

    ActiveRecord::Schema.define(version: 20141127114323) do
    create_table "contact_requests", force: true do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "message", null: false
    t.integer "user_id"
    end

    add_index "contact_requests", ["user_id"], name: "index_contact_requests_on_user_id", using: :btree

    create_table "users", force: true do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "first_name", null: false
    t.string "last_name", null: false
    t.string "email", null: false
    t.string "phone_number"
    end
    end

    Here are my models and controllers:

    class User < ActiveRecord::Base
    has_many :contact_requests
    # validate the presence of the attributes
    validates(:first_name, presence: true)
    validates(:last_name, presence: true)
    validates(:email, presence: true)
    end

    class ContactRequest < ActiveRecord::Base
    belongs_to :user
    validates :user_id, presence: true
    validates :message, presence: true, length: { maximum: 500 }
    end

    class ContactController < ApplicationController
    def new
    @user = User.new
    @contact_request = ContactRequest.new
    end
    end


    I am getting this error:

    <https://lh4.googleusercontent.com/-MRCqb9NOm6A/VHpTtDohT-I/AAAAAAAAAGE/hqgxKaH07bE/s1600/Screen%2BShot%2B2014-11-29%2Bat%2B2.34.29%2BPM.png>



    form:

    <%= form_for(:contact, remote: true, class: "contact-input") do |f| %>
    <div class="col-md-12">
    <div class="col-md-6">
    <div class="contact-input-margin form-group">
    <%= f.text_field(:user, :first_name, class: "form-control", placeholder: "First name")%>
    </div>
    <div class="contact-input-margin form-group">
    <%= f.text_field(:user, :last_name, class: "form-control", placeholder: "Last name") %>
    </div>
    <div class="contact-input-margin form-group">
    <%= f.email_field(:user, :email, class: "form-control", placeholder: "Email") %>
    </div>
    <div class="contact-input-margin form-group">
    <%= f.telephone_field(:user, :phone_number, class: "form-control", placeholder: "Phone number") %>
    </div>
    </div>
    <div class="contact-input-margin col-md-6">
    <div class="form-group">
    <%= f.text_area(:contact_request, :message, class: "form-control contact-margin", rows: "8", placeholder: "Message...") %>
    </div>
    </div>
    </div>
    <%= f.submit(class: "btn btn-xl") %>
    <% end %>



    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com To post to this group, send email to rubyonrails-talk@googlegroups.com To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6f73f2d0-e80d-4ff1-89a0-1e1b63ffa3ac%40googlegroups.com <https://groups.google.com/d/msgid/rubyonrails-talk/6f73f2d0-e80d-4ff1-89a0-1e1b63ffa3ac%40googlegroups.com?utm_medium=email&utm_source=footer>.
    For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
    ----

    Jason Fleetwood-Boldt
    tech@datatravels.com
    http://www.jasonfleetwoodboldt.com/writing

    All material © Jason Fleetwood-Boldt 2014. Public conversations may be turned into blog posts (original poster information will be made anonymous). Email jason@datatravels.com with questions/concerns about this.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/457AF6FB-29C4-41CE-A0F6-7CD6D8C21FC1%40datatravels.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 29, '14 at 11:21p
activeNov 30, '14 at 2:50a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase