Change this line in the controller
@user = User.new(params[user_params])
to
@user = User.new(user_params)
user_params
is a method, not an item of the params array.
题
I'm fairly new to Rails and have recently created an end to end simple application, with a Rails backend and a front end iOS app that consume my backend JSON API.
I have no problem creating data via a POST for all my models.
However, I've recently decided to add a User model (as per Hartl's tutorial), and I can't seem to create a new entry via Postman. Here's what I've got...
api/v1/users_controller.rb
module Api
module V1
class UsersController < ApplicationController
respond_to :json
skip_before_filter :verify_authenticity_token
def show
@user = User.find(params[:id])
respond_with (@user)
end
def create
@user = User.new(params[user_params])
if @user.save!
respond_with (@user)
else
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
end
end
And models/user.rb
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
I fire up my server, and post the following via PostMan...
{
"user":
{
"name": "Johnny",
"email": "J@J.com",
"password": "aaaaaa",
"password_confirmation": "aaaaaa"
}
}
But i get this output on the server...
Started POST "/api/v1/users" for 127.0.0.1 at 2014-05-13 09:04:23 -0700
Processing by Api::V1::UsersController#create as JSON
Parameters: {"user"=>{"name"=>"Johnny", "email"=>"J@J.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
(0.3ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(0.4ms) ROLLBACK
Completed 422 Unprocessable Entity in 12ms
ActiveRecord::RecordInvalid (Validation failed: Name can't be blank, Email can't be blank, Email is invalid, Password can't be blank, Password is too short (minimum is 6 characters)):
app/controllers/api/v1/users_controller.rb:14:in `create'
If I change save! to save, I get...
Started POST "/api/v1/users" for 127.0.0.1 at 2014-05-13 09:06:09 -0700
Processing by Api::V1::UsersController#create as JSON
Parameters: {"user"=>{"name"=>"Johnny", "email"=>"J@J.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
(0.1ms) BEGIN
User Exists (1.6ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(0.2ms) ROLLBACK
Completed 500 Internal Server Error in 95ms
ActionView::MissingTemplate (Missing template api/v1/users/create, application/create with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/Users/Developer/RUBY/iOSandRails/hotelDemo/app/views"
):
What am I missing here?
解决方案
Change this line in the controller
@user = User.new(params[user_params])
to
@user = User.new(user_params)
user_params
is a method, not an item of the params array.