Jake Smith
Jake Smith

Reputation: 2813

Rails 4 form select inputs giving "can't be blank" error when they aren't blank

I'm using Rails 4 and I have two select inputs in my signup form. When I make a selection and fill out the rest of the form, I get two errors associated with these inputs that say they cannot be blank. I'm not sure why I'm getting these errors if they aren't blank...

Here are the gems that I'm using that are relevant:

Gemfile

gem 'bootstrap_form'
gem 'twitter_cldr'

Here is the partial with the fields I'm using:

app/views/users/_fields.html.erb

<%= render 'shared/error_messages', object: f.object %>

<%= f.text_field :name, hide_label: true, placeholder: 'name' %>
<%= f.text_field :email, hide_label: true, placeholder: 'email' %>
<%= f.select :language1, TwitterCldr::Shared::Languages.all.values %>
<%= f.select :language2, TwitterCldr::Shared::Languages.all.values %>
<%= f.password_field :password, hide_label: true, placeholder: 'password' %>
<%= f.password_field :password_confirmation, hide_label: true, placeholder: 'confirm password' %>

The columns of the users table is defined as such:

db/schema.rb

...
create_table "users", force: true do |t|
...
    t.string "language1"
    t.string "language2"
...
end

and here is the validation in my user model:

app/models/user.rb

validates :language1, presence: true
validates :language2, presence: true

My thoughts:

What I'm thinking is that what is being selected in the dropdown is maybe not a string? That or I have seen that in some cases you have to specify the object that the form is for in the select method like this:

<%= f.select :user, :language1, TwitterCldr::Shared::Languages.all.values %>

but my problem with this approach is that these fields are in a partial and the object is passed in as a local, so when I do this:

<%= f.select f.object, :language1, TwitterCldr::Shared::Languages.all.values %>

I get an error about an undefined method 'to_sym' for my class of User

Anybody have any suggestions??? Thanks!

Upvotes: 1

Views: 4964

Answers (2)

Divine
Divine

Reputation: 11

I have same issues as well. Initially, I naively put

def create

@micropost = user.microposts.create(params[:microposts])

And when I put

 @micropost = user.microposts.create(micropost_params), 

private
def micropost_params
     params.require(:micropost).permit(:content, :user_id)
end

it works.

But why....

Upvotes: 0

Tom Prats
Tom Prats

Reputation: 7921

This is most likely a controller error. Make sure that strong params is accepting the attributes:

params.require(:user).permit(:name, :email, :password, :language1, :language2)

Look at strong params

Upvotes: 12

Related Questions