1

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 &#39;{&quot;user&quot;:{&quot;firstName&quot;:&quot;John&quot;, &quot;lastName:&quot;Doe&quot;, &quot;userName&quot;:&quot;johndoe&quot;, &quot;password&quot;:&quot;mypassword&quot;, &quot;email&quot;:&quot;johndoe@gmail.com&quot;}}&#39;</h2>

UPDATE:

Now I get the following error after I got the JSON correct:

ActionController::InvalidAuthenticityToken in UsersController#create

john doe
  • 9,220
  • 23
  • 91
  • 167

1 Answers1

1

The error you are encountering is from Strong Parameters, which is included in Rails by default for Rails 4. Try using the following code in your controller instead to allow only certain parameters:

def create
  @user = User.new(user_params)
  @user.save

  respond_to do |format|
    format.json { render :json => @user }
  end
end

private

  def user_params
    params.require(:user).permit(:firstName, :lastName, :userName, :password, :email)
  end
jvperrin
  • 3,368
  • 1
  • 23
  • 33
  • Thanks I got it working! but does the create action takes in json parameters. I want to take json parameters and then convert that to user object and then save the user object. – john doe Oct 28 '13 at 02:32
  • The form will send the parameters using a `POST` request, so it does not use json parameters, but it will work to create a new user. Is the form you showed the only place to expect to be making new users from, or do you need to make an API with your application? – jvperrin Oct 28 '13 at 02:35
  • I need to make an API with my application and then use it with my iOS app! Thanks for your help. First day with Rails :( – john doe Oct 28 '13 at 02:36
  • 1
    You should be able to send JSON by default, without having to change anything, as long as you use the right headers in your request. I have updated the controller code so that the rails application will respond with a JSON encoded representation of the user that was just saved in response. Keep going on Rails, it gets better, and it is actually surprisingly fun to work with once you get used to it! – jvperrin Oct 28 '13 at 02:44
  • Please see the updated question. When I pass json it is not getting saved! – john doe Oct 28 '13 at 02:50
  • Also I cannot do respond_to inside the create method maybe because create method is a POST method and not GET. – john doe Oct 28 '13 at 02:53
  • `respond_to` works fine within a create action, it just specifies what happens when the action finishes, like what to send as a response. Also, pass this JSON instead, as the parameters have to be nested when creating a new resource: `{"user":{"firstName":"John", "lastName:"Doe", "userName":"johndoe", "password":"mypassword", "email":"johndoe@gmail.com"}}` There may be problems with the `authenticity_token`, but that can be worked on if this works. – jvperrin Oct 28 '13 at 02:58
  • Updated the question with new update! I am now passing the json u sent me but I get another error "bad request". see updated question. – john doe Oct 28 '13 at 03:02
  • Okay I was sending invalid JSON. I got it validate and now I get a new error. Please see update in the question – john doe Oct 28 '13 at 03:05
  • Try adding `protect_from_forgery with: :null_session` into your `ApplicationController`. See [this question](http://stackoverflow.com/questions/16258911/rails-4-authenticity-token) for more details. – jvperrin Oct 28 '13 at 03:10
  • Thanks! Now when I invoke the create action which is POST I get missing templates for create! This is getting annoying! Maybe rails is not for me! – john doe Oct 28 '13 at 03:15
  • 1
    What are you using to send your requests? You need to have the appropriate headers: `{'ACCEPT' => "application/json", 'CONTENT_TYPE' => 'application/json'}` so that rails knows what you want back. I think right now it assumes you want html in response, but you don't have a html template. Rails is complicated, and you are starting out with one of the more complicated parts of Rails as well, so I wouldn't get discouraged. – jvperrin Oct 28 '13 at 03:23
  • I solved it! Just had to render the response! Thanks a million for your help – john doe Oct 28 '13 at 03:24
  • You're welcome, I'm glad I could help! I hope you have more luck with Rails in the future. – jvperrin Oct 28 '13 at 03:25