I have a simple new form like this:
<h1> New </h1>
<%= form_for :user, url: 'create' do |f| %>
<p>
<%= f.label :firstName %>
<%= f.text_field :firstName %>
</p>
<p>
<%= f.label :lastName %>
<%= f.text_field :lastName %>
</p>
<p>
<%= f.label :userName %>
<%= f.text_field :userName %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
As you can see it invokes the create action which is defined below:
def create
@user = User.new(params[:user])
@user.save
end
It gives me the following error:
ActiveModel::ForbiddenAttributesError
Extracted source (around line #8):
6
7
8
9
10
11
def create
@user = User.new(params[:user])
@user.save
end
All I want is that my method create accept input as json arguments and then convert those arguments to a user object and then save the object.
UPDATE:
I have the following create method which is defined as post:
def create
@user = User.new(user_params)
@user.save
end
I am sending the following json but it never gets saved:
{"firstName":"John", "lastName:"Doe", "userName":"johndoe", "password":"mypassword", "email":"johndoe@gmail.com"}
UPDATE:
After passing the following JSON:
{"user":{"firstName":"John", "lastName:"Doe", "userName":"johndoe", "password":"mypassword", "email":"johndoe@gmail.com"}}
I get the following error in my HTTPClient application:
<h2>795: unexpected token at '{"user":{"firstName":"John", "lastName:"Doe", "userName":"johndoe", "password":"mypassword", "email":"johndoe@gmail.com"}}'</h2>
UPDATE:
Now I get the following error after I got the JSON correct:
ActionController::InvalidAuthenticityToken in UsersController#create