0

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 later on, I would like to include a mailer within the form as well but one step at a time.

I currently have a user table and a contact_request table:

enter image description here

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:

enter image description here

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 %>

1 Answer 1

1

This error is happening becuase you're specifying :user as the first argument in text_field:

<%= f.text_field(:user, :first_name, class: "form-control", placeholder: "First name")%>

You don't need to do this, as form_for takes care of this for you. Just this code will do:

<%= f.text_field(:first_name, class: "form-control", placeholder: "First name")%>

Remove :user from all your other fields too.

2
  • Ryan-I think he also needs to put accepts_nested_attributes_for. Also that will mean you will probably want a syntax like <%= f.text_field("user[first_name]", ... %>
    – Jason FB
    Nov 30, 2014 at 2:46
  • @JasonFB I would use a fields_for for that, rather than putting the user parameter.
    – Ryan Bigg
    Nov 30, 2014 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.