# Getting a 'State can't be blank' validation error on submit

**URL:** <https://www.ruby-forum.com/t/getting-a-state-cant-be-blank-validation-error-on-submit/122216>\
**Category:** Rails\
**Created:** [December 5, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/getting-a-state-cant-be-blank-validation-error-on-submit/122216 "2007-12-05T17:44:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_ASRichman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Mark\_ASRichman](https://www.ruby-forum.com/u/Mark_ASRichman)\
**Post date:** [December 5, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/getting-a-state-cant-be-blank-validation-error-on-submit/122216/1 "2007-12-05T17:44:08Z")

</div>

See [http://pastie.caboo.se/124961](http://pastie.caboo.se/124961)

I’m basically just trying to validate the Users.state\_id field. The  
Users table holds the FK to States as user.state\_id, where there is a  
one-to-many between States and Users.

# state.rb:

class State \< ActiveRecord::Base  
has\_many :users  
end

# user.rb:

class User \< ActiveRecord::Base  
…  
belongs\_to :state  
…  
validates\_presence\_of :state\_id  
validates\_associated :state  
…  
end

# user\_controller.rb:

class UserController \< ApplicationController  
…  
def signup  
…  
@user = User.new(params[:user])  
return unless request.post?  
@user.save!  
self.current\_user = @user  
redirect\_back\_or\_default(:controller =\> ‘/user’, :action =\>  
‘index’)  
flash[:notice] = “Thanks for signing up!”  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘signup’  
end  
end

signup.rhtml:  
…  
\<%= error\_messages\_for :user %\>  
\<% form\_for :user, :url =\> {:action =\> “signup” } do |f| -%\>  
…  
\<%= f.collection\_select :state\_id, State.find\_all, :id, :name,  
{:include\_blank =\> true}, {:style =\> ‘background-color: rgb(255, 255,  
160);’, :class =\> ‘form-txt2’} %\>  
…

development.log:  
…  
Processing UserController#signup (for 127.0.0.1 at 2007-12-05  
09:57:40) [POST]  
Session ID: 815f814a0b1d8cb815f0e047a920f142  
Parameters: {“user”=\>{“city”=\>“”, “born\_on(1i)”=\>“1907”,  
“postal\_code”=\>“”, “born\_on(2i)”=\>“12”, “looking\_for\_id”=\>“1”,  
“headline”=\>“”, “height\_inches\_id”=\>“1”, “password\_confirmation”=\>“”,  
“country”=\>“United States”, “have\_children\_id”=\>“1”,  
“born\_on(3i)”=\>“5”, “smoke\_id”=\>“1”, “terms\_of\_service”=\>“0”,  
“want\_children\_id”=\>“1”, “ethnicity\_id”=\>“1”, “drink\_id”=\>“1”,  
“description”=\>“”, “marital\_status\_id”=\>“1”, “body\_type\_id”=\>“1”,  
“interests\_string”=\>“”, “hair\_color\_id”=\>“1”, “login”=\>“”,  
“password”=\>“”, “religion\_id”=\>“1”, “email”=\>“”, “state”=\>“18”,  
“profession”=\>“”}, “typed\_code”=\>“”, “x”=\>“30”, “y”=\>“11”,  
“action”=\>“signup”, “controller”=\>“user”, “picture”=\>“”,  
“preference”=\>{“receive\_jsh\_mail”=\>“1”}}  
State Load (0.000000) SELECT \* FROM states  
…
