I have a model called AlphaUser. When I submit a form to create a new alpha user, I'm told there wasn't any data in the params[:post]. How can this happen?
My code for the form:
<%= form_for :alpha_user,
url: alpha_users_path,
class: "form",
id: "alphaTest" do |f| %>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email,
class: "form-control", placeholder: "grandma@aol.com" %>
<%= f.text_field :producer,
class: "hidden", value: "0" %>
</div>
<%= f.submit "Request Access", class: "btn btn-primary" %>
<% end %>
My controller:
class AlphaUsersController < ApplicationController
def create
render text: params[:post].inspect
end
end
Data is still recieved, just in this format:
{"utf8"=>"✓", "authenticity_token"=>"blahblahblah", "alpha_user"=>{"email"=>"test@test.com", "producer"=>"0"}, "commit"=>"Request Access", "action"=>"create", "controller"=>"alpha_users"}
You need to call params for the :alpha_user... e.g. params[:alpha_user].
If you're using strong_parameters in Rails 4, this should do the trick:
class AlphaUsersController < ApplicationController
def create
@alpha_user = AlphaUser.new(alpha_user_params)
@alpha_user.save
end
private
def alpha_user_params
params.require(:alpha_user).permit(:email, :producer)
end
end
Notice that in your create action you are no longer referencing the params directly, but the alpha_user_params method required by strong_parameters.