Home undefined method 'model_name' for nil:NilClass
Reply: 0

undefined method 'model_name' for nil:NilClass

user5330
1#
user5330 Published in June 19, 2018, 6:30 pm

I am trying to get address of users and save it in database but i am getting undefined method 'model_name' for nil:NilClass. In the same page the name can updated which is got during signup. I want name to be stored in users table and the address to be stored in addresses table in the database when the save changes button is presseed. I dont know where i am going wrong. please anyone help in fixing this issue!

View Code

<%= form_for(@user, url: { action: 'update_profile' }, html: { class: 'm-form m-form--fit m-form--label-align-right' } ) do |f| %>
  <% if @user.errors.any? %>
    <h4><%= pluralize(@user.errors.count, "error") %>
      prohibited this profile from being saved:</h4>
    <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  <% end %>
  <div class="form-group m-form__group row">
    <label for="name" class="col-2 col-form-label">
      Name
    </label>
    <div class="col-7">
      <%= f.text_field :name, class: 'form-control m-input', placeholder: 'Full Name' %>
    </div>
  </div>
  <div class="m-form__seperator m-form__seperator--dashed m-form__seperator--space-2x"></div>
  <%= f.fields_for @user.address do |a| %>

  <div class="form-group m-form__group row">
    <label for="example-text-input" class="col-2 col-form-label">
      Address
    </label>
    <div class="col-7">
      <%= a.text_field :area, class: 'form-control m-input', placeholder: 'Address' %>
    </div>
  </div>
  <div class="form-group m-form__group row">
    <label for="example-text-input" class="col-2 col-form-label">
      City
    </label>
    <div class="col-7">
      <%= a.text_field :city, class: 'form-control m-input', placeholder: 'City' %>
    </div>
  </div>
  <div class="form-group m-form__group row">
    <label for="example-text-input" class="col-2 col-form-label">
      State
    </label>
    <div class="col-7">
      <%= a.text_field :state, class: 'form-control m-input', placeholder: 'State' %>
    </div>
  </div>
<% end %>
<%= f.submit 'Save Changes', class: 'btn btn-accent m-btn m-btn--air m-btn--custom' %>
&nbsp;&nbsp;
<%= link_to 'Back', root_path, class: 'btn btn-secondary m-btn m-btn--air m-btn--custom' %>

Controller code

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

  def index; end

  def update_profile
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to profile_index_path, notice: 'Profile was successfully updated.' }
      else
        format.html { render :index }
      end
    end
  end

private

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

  def user_params
    params.require(:user).permit(:name)
  end

  def address_params
    params.require(:user).permit(address: %i[area state country])
  end
end

Model code

Address.rb

class Address < ApplicationRecord
  belongs_to :user
end

User.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :address, dependent: :destroy

  validates :name, presence: true
  def admin?
    false
  end
end

Terminal log

Started GET "/profile" for 127.0.0.1 at 2018-05-02 14:31:21 +0530
Processing by ProfileController#index as HTML
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 11 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.2ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 11 LIMIT 1
  Rendering profile/index.html.erb within layouts/application
  Rendered profile/_profile_card.html.erb (0.2ms)
  Address Load (0.2ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 11 LIMIT 1
  Rendered profile/_profile_detail.html.erb (3.4ms)
  Rendered profile/index.html.erb within layouts/application (4.7ms)
Completed 401 Unauthorized in 8ms (ActiveRecord: 0.7ms)


[Rollbar] Reporting exception: undefined method `model_name' for nil:NilClass
[Rollbar] Scheduling item
[Rollbar] Sending item
[Rollbar] Success
[Rollbar] Details: https://rollbar.com/instance/uuid?uuid=60dbe4e1-e5ef-4414-a9a5-e8a866c47740 (only available if report was successful)
[Rollbar] Exception uuid saved in env: 60dbe4e1-e5ef-4414-a9a5-e8a866c47740

NoMethodError - undefined method `model_name' for nil:NilClass:
  app/views/profile/_profile_detail.html.erb:45:in `block in _app_views_profile__profile_detail_html_erb___3163788399910446599_70315531332300'
  app/views/profile/_profile_detail.html.erb:26:in `_app_views_profile__profile_detail_html_erb___3163788399910446599_70315531332300'
  app/views/profile/index.html.erb:7:in `_app_views_profile_index_html_erb__1161982046354726382_70315531116820'

Started POST "/__better_errors/0227f950024c358e/variables" for 127.0.0.1 at 2018-05-02 14:31:22 +0530
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.33403 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO