本文介绍了未定义的方法 'model_name' 为 nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取用户的地址并将其保存在数据库中,但我得到了 undefined method 'model_name' for nil:NilClass
.在同一页面中,名称可以更新,这是在注册过程中获得的.当按下保存更改按钮时,我希望将名称存储在用户表中,并将地址存储在数据库的地址表中.我不知道我哪里出错了.请任何人帮助解决此问题!
查看代码
<%= form_for(@user, url: { action: 'update_profile' }, html: { class: 'm-form m-form--fit m-form--label-align-right' } ) 做 |f|%><% if @user.errors.any?%><h4><%=pluralize(@user.errors.count, "error") %>禁止保存此配置文件:</h4><ul><% @user.errors.full_messages.each 做 |message|%><li><%=消息%></li><%结束%><%结束%><div class="form-group m-form__group row"><label for="name" class="col-2 col-form-label">名称标签><div class="col-7"><%= f.text_field :name, class: 'form-control m-input', placeholder: 'Full Name' %>
<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">地址标签><div class="col-7"><%= a.text_field :area, class: 'form-control m-input', placeholder: 'Address' %>
<div class="form-group m-form__group row"><label for="example-text-input" class="col-2 col-form-label">城市标签><div class="col-7"><%= a.text_field :city, class: 'form-control m-input', placeholder: 'City' %>
<div class="form-group m-form__group row"><label for="example-text-input" class="col-2 col-form-label">状态标签><div class="col-7"><%= a.text_field :state, class: 'form-control m-input', placeholder: 'State' %>
<%结束%><%= f.submit 'Save Changes', class: 'btn btn-accent m-btn m-btn--air m-btn--custom' %> <%= link_to 'Back', root_path, class: 'btn btn-secondary m-btn m-btn--air m-btn--custom' %>
控制器代码
class ProfileController
型号代码
地址.rb
类地址
用户.rb
class User
终端日志
在 2018-05-02 14:31:21 +0530 开始为 127.0.0.1 获取/profile"由 ProfileController#index 处理为 HTML用户负载 (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 11 ORDER BY `users`.`id` ASC LIMIT 1用户负载 (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 11 LIMIT 1在 layouts/application 中渲染 profile/index.html.erb渲染的 profile/_profile_card.html.erb (0.2ms)地址负载 (0.2ms) SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 11 LIMIT 1渲染的 profile/_profile_detail.html.erb (3.4ms)在布局/应用程序中渲染 profile/index.html.erb(4.7 毫秒)8ms 内完成 401 Unauthorized (ActiveRecord: 0.7ms)[Rollbar] 报告异常:未定义方法`model_name' for nil:NilClass[滚动条] 调度项目[滚动条] 发送项目[滚动条] 成功[Rollbar] 详情:https://rollbar.com/instance/uuid?uuid=60dbe4e1-e5ef-4414-a9a5-e8a866c47740(仅在举报成功时可用)[Rollbar] 异常 uuid 保存在 env 中:60dbe4e1-e5ef-4414-a9a5-e8a866c47740NoMethodError - nil:NilClass 的未定义方法model_name":app/views/profile/_profile_detail.html.erb:45:在`阻止_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:在`_app_views_profile_index_html_erb__1161982046354726382_70315531116820'在 2018-05-02 14:31:22 +0530 为 127.0.0.1 开始 POST "/__better_errors/0227f950024c358e/variables"
解决方案
您的@address 可能为零.必须先在控制器或视图中初始化
class UsersController
或在上面的视图中:
<%= f.fields_for :address do |a|%>
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' %>
<%= 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
解决方案
Your @address is probably nil. You must initialize it first in the controller or view
class UsersController < ApplicationController
def set_user
@user = User.find(current_user.id)
@user.address || @user.build_address
end
end
or in the view above:
<% @user.address || @user.build_address %>
<%= f.fields_for :address do |a| %>
这篇关于未定义的方法 'model_name' 为 nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-05 01:43