我正在尝试创建一个表单,它会在我的表中添加一行但是我收到此错误并且我不确定原因:
Started POST "/test" for ::1 at 2014-11-30 01:51:49 -0800
Processing by UsersController#create as JS
Parameters: {"utf8"=>"✓", "users"=>{"first_name"=>"brady", "last_name"=>"LIII", "email"=>"brad@gmail.com", "phone_number"=>"123456789"}, "commit"=>"Save Users"}
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/users_controller.rb:32:in `create'
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_source.erb (2.0ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_trace.text.erb (0.9ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.5ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/diagnostics.text.erb (11.5ms)
::1 - - [30/Nov/2014:01:51:49 -0800] "POST /test HTTP/1.1" 500 92618 0.0449
我的表格:
<!-- 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>
<%= f.submit(class: "btn btn-xl") %>
<% end %>
</div>
</section>
我的控制器:
class UsersController < ApplicationController
def new
end
def create
if !(User.find_by(email: params[:user][:email]))
@user = User.new(user_params)
@user.save
end
end
private
def user_params
params.require(:users).permit(:first_name, :last_name, :email, :phone_number)
end
end
我的用户模型
class User < ActiveRecord::Base
before_save { |user| user.email = user.email.downcase }
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
我的路线
Rails.application.routes.draw do
root 'home#index'
get 'test', to: "users#new"
post 'test', to: "users#create"
模式:
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