0

I am getting the error uninitialized constant Degree. I have a column in database with column name type. When i give data to that field and save, the data i gave is getting saved in database but error message is displayed after that and i am not able to reload that page.

Controller code

class ProfileController < ApplicationController
  before_action :set_user, only: %i[index update_profile]

  def index; end 
  def update_profile
    if @user.update(user_params)
      redirect_to profile_index_path, notice: 'Profile was successfully updated.'
    else
      render :index
    end
  end
private

  def set_user
    @user = User.find(current_user.id)
    @user.education || @user.build_education
  end

  def user_params
    params.require(:user).permit(:name, education_attributes: %i[id type name issue_institute education_status])
  end
end

education.rb

class Education < ApplicationRecord
  belongs_to :user

  validates_presence_of :user_id
end

user.rb

class User < ApplicationRecord
  has_one :education, dependent: :destroy
  accepts_nested_attributes_for :education
end

View code

<%= form_for(@user, url: {action: 'update_profile'}, html: {class: 'm-form m-form--fit m-form--label-align-right'}) do |f| %>
<%= f.fields_for :education, @user.education do |e| %>
<%= e.select :type, options_for_select(%w(Degree Certification), params[:type]), prompt: 'Degree/Certification', class: 'form-control m-input' %>
<%= end %>
<%= f.submit 'Save Changes'%>
<%= end %>

Terminal Log when i save that field

Started PATCH "/profile/update_profile" for 127.0.0.1 at 2018-05-30 09:04:37 +0530
Processing by ProfileController#update_profile as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9QiEdSxqwkhHqZHiraiQjJcUvUS+oJknYjYaxWUSQrh+je0ASeYQvs//Z+p+oZkOqyAiwxc3nsxp/iohO9B1BA==", "user"=>{"name"=>"Admin", "email"=>"admin@gmail.com", "address_attributes"=>{"area"=>"5, nehru Street", "city"=>"pune", "state"=>"mumbai", "country"=>"india", "postcode"=>"626781", "id"=>"1"}, "education_attributes"=>{"type"=>"Degree", "name"=>"ffgxh", "issue_institute"=>"", "education_status"=>"", "id"=>"1"}, "fee_attributes"=>{"fee_hour"=>"", "fee_month"=>"", "id"=>"1"}}, "commit"=>"Save Changes"}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.2ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  Address Load (0.2ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 2 LIMIT 1
  Fee Load (0.2ms)  SELECT  `fees`.* FROM `fees` WHERE `fees`.`user_id` = 2 LIMIT 1
  Education Load (0.2ms)  SELECT  `educations`.* FROM `educations` WHERE `educations`.`user_id` = 2 LIMIT 1
Unpermitted parameter: :email
   (0.2ms)  BEGIN
  SQL (0.4ms)  UPDATE `educations` SET `type` = 'Degree', `updated_at` = '2018-05-30 03:34:37' WHERE `educations`.`id` = 1
   (5.3ms)  COMMIT
Redirected to http://localhost:3000/profile
Completed 302 Found in 19ms (ActiveRecord: 7.1ms)


Started GET "/profile" for 127.0.0.1 at 2018-05-30 09:04:37 +0530
Processing by ProfileController#index as HTML
  User Load (0.7ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  Address Load (0.4ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 2 LIMIT 1
  Fee Load (0.3ms)  SELECT  `fees`.* FROM `fees` WHERE `fees`.`user_id` = 2 LIMIT 1
  Education Load (0.8ms)  SELECT  `educations`.* FROM `educations` WHERE `educations`.`user_id` = 2 LIMIT 1
Completed 401 Unauthorized in 12ms (ActiveRecord: 2.4ms)



NameError - uninitialized constant Degree:
  app/controllers/profile_controller.rb:41:in `set_user'

Data gets saved but i have error page after that. Can someone help me with it? Thanks in advance.

Harini
  • 109
  • 1
  • 3
  • 12
  • So use just want to use the "type" column as a string and you do not want to use single table inheritance? Can you try setting `self.inheritance_column = nil` in the User model? – Jon May 30 '18 at 04:35
  • 1
    `type` is a reserved keyword for AR. http://reservedwords.herokuapp.com/words?utf8=%E2%9C%93&q%5Bword_or_notes_cont%5D=type. Change the column name. – Emu May 30 '18 at 04:39
  • ... or tell Rails that `type` is not your inheritance column, see [Rails — use type column without STI?](https://stackoverflow.com/q/7134559/477037) – Stefan May 30 '18 at 04:42
  • @Emu thank you it worked. – Harini May 30 '18 at 05:12

1 Answers1

1

I am posting my comment as an answer so that anyone can reference that in future:

type is a reserved keyword for AR. Check list of reserve active record keywords from here. Change the column name. It will resolve the error.

Emu
  • 5,763
  • 3
  • 31
  • 51