0
votes

I am trying to sign up using devise for an application I am making. The form submit action is sending the correct params but I am failing on one presence validation. That presence validation is a checkbox field where a user selects on sign up whether they are single or not.

Here is the code for views/devise/registrations/new.html.erb

<div class="radio">
  <%= f.label :relationship_status%><br />
  <%= radio_button_tag(:relationship_status, "single") %>
  <%= label_tag(:single, "Single") %>
  <%= radio_button_tag(:relationship_status, "relationship") %>
  <%= label_tag(:relationship, "In a relationship") %>
</div>

Here is the ApplicationController where I specify the params to be passed aside from the defaults provided by devise:

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name,
                                      :username, :birthday,
                                      :relationship_status])

  end
end

Here is my user model where I set basic presence validators

class User < ApplicationRecord

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :validatable
   validates :name, :email, :username, 
   :birthday,:relationship_status, presence: true
end

Finally, here is an image of clicking Sign Up once I fill the form: enter image description here

ActiveRecord::Schema.define(version: 2019_06_05_024141) do

  create_table "users", force: :cascade do |t|
    t.string "name", null: false
    t.string "username", null: false
    t.datetime "birthday", null: false
    t.string "relationship_status", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end
end

As you can see in the picture relationship_status is clearly set to single. Yet I keep getting an error saying that it can't be blank. I am stumped and some help would be appreciated. Thank you.

2
could you show the schema fileKamal Pandey
Can you post user schema code?Anand
Thanks for looking into this guys. The schema code has been posted.Dan Rubio
I don't see any issue with the schema file did you restart the app after changes has been made.Kamal Pandey
@DanRubio How can you be sure, relationship_status is cause of rollback? have you tried with the debugger? So far i can see your birthday is making issue.Anand

2 Answers

0
votes

could you try with this.

<div class="radio">
  <%= f.label :relationship_status%><br />
  <%= f.radio_button(:relationship_status, "single") %>
  <%= label_tag(:single, "Single") %>
  <%= f.radio_button(:relationship_status, "relationship") %>
  <%= label_tag(:relationship, "In a relationship") %>
</div>
0
votes

I don't know how this was fixed, but my approach was to confirm with byebug. While doing so, I executed rails generate devise:controllers [scope] and modified my route.rb with the following:

Rails.application.routes.draw do
  devise_for :users, controllers: {
    sessions: 'users/sessions'
  }
end

This fixed it.