通过表单更新两个具有一对多关系的表

时间:2014-12-02 08:24:53

标签: ruby-on-rails

我正在尝试通过表单更新我的两个表,但是我收到了这个错误:

Started POST "/test" for ::1 at 2014-12-02 00:15:21 -0800
Processing by UsersController#create as JS
  Parameters: {"utf8"=>"✓", "users"=>{"first_name"=>"sdfdsfdsf", "last_name"=>"dsfdsfds", "email"=>"3213213@hotmail.com", "phone_number"=>"23123213", "message"=>"sdfdsfsdfdsfkjsdfksdfk;adklsfjksadfksjdfklsdf"}, "commit"=>"Save Users"}
Completed 500 Internal Server Error in 1ms

ArgumentError (wrong number of arguments (1 for 0)):
  app/controllers/users_controller.rb:32:in `create'


  Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (2.6ms)
  Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.text.erb (0.4ms)
  Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.6ms)
  Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/diagnostics.text.erb (12.2ms)

我一直在阅读示例和文档,我认为我正确地编写了表单,但我认为我的控制器缺少某些方面,因为我不熟悉rails。

enter image description here

这是我的架构:

ActiveRecord::Schema.define(version: 20141130075753) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "contact_requests", force: true do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text     "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
  validates(:first_name, presence: true)
  validates(:last_name, presence: true)
  validates(:email, presence: true)
  accepts_nested_attributes_for :contact_requests

end

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

用户控制器:

  def create
    if !(User.find_by(email: params[:users][:email]))
      @user = User.new(user_params)
      @contact_request = ContactRequest.new(contact_request_params)
      @contact_request.save
      @user.save
    else
      @contact_request = ContactRequest.new(contact_request_params)
      @contact_request.save
    end

  end

  private

    def user_params
      # strong_parameters, which requires us to tell Rails exactly which parameters
      # we want to accept in our controllers
      params.require(:users).permit(:first_name, :last_name, :email, :phone_number)
    end

end

形式:

<!-- contact -->
<section id="contact">
  <div class="container">
    <div class="title-container">Contact Us</div>
    <div class="title-caption">Reach us at (415)-911-9999</div>
    <%= form_for(:users, remote: true, id: "contact-form", 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(:first_name, class: "form-control", placeholder: "First name")%>
          </div>
          <div class="contact-input-margin form-group">
            <%= f.text_field(:last_name, class: "form-control", placeholder: "Last name") %>
          </div>
          <div class="contact-input-margin form-group">
            <%= f.email_field(:email, class: "form-control", placeholder: "Email") %>
          </div>
          <div class="contact-input-margin form-group">
            <%= f.telephone_field(:phone_number, class: "form-control", placeholder: "Phone number") %>
          </div>
        </div>
        <div class="contact-input-margin col-md-6">
          <div class="form-group">
            <%= f.fields_for :contact_requests do |builder| %>
              <%= f.text_area(:message, class: "form-control contact-margin", rows: "8", placeholder: "Message...") %>
              <% end %>
          </div>
        </div>
      </div>
      <%= f.submit(class: "btn btn-xl") %>
    <% end %>
  </div>
</section>

0 个答案:

没有答案