创建更新2个表的联系表单

时间:2014-11-29 22:58:35

标签: ruby-on-rails forms postgresql

我正在尝试创建一个更新数据库中2个表的表单。我还不太确定如何构建这个联系表单。所以我的第一个短期目标是简单地更新我的contact_requestsusers表。我的目标是在one to manyusers之间建立contact_requests关系。这允许一个user拥有多个contact_requests。我是rails的新手,所以我觉得我的代码中存在空白,我也犯了一些错误。理想情况下,我希望在表单中包含一个邮件程序,但一次只能一步。

我目前有一个user表和一个contact_request表:

enter image description here

来自user_id表的

contact_requests是引用users.id的外键

以下是当前架构代码:

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

以下是我的模型和控制器:

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

我收到此错误:

enter image description here

形式:

<%= 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 个答案:

答案 0 :(得分:1)

发生此错误的原因是您将:user指定为text_field中的第一个参数:

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

您无需执行此操作,因为form_for会为您解决此问题。只需这段代码即可:

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

从所有其他字段中删除:user