我正在关注我们正在创建和更新用户帐户的教程,并且我坚持编辑部分,因为当我尝试为用户加载编辑页面时,我得到no方法错误,如下所示:
Showing /home/ubuntu/rails_projects/alpha_blog/app/views/shared/_errors.html.erb where line #1 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #1):
1
2
3
4
5
6
<% if obj.errors.any? %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="panel panel-danger">
<div class="panel-heading">
<h2 class="panel-title">
Trace of template inclusion: app/views/users/_form.html.erb, app/views/users/edit.html.erb
Rails.root: /home/ubuntu/rails_projects/alpha_blog
Application Trace | Framework Trace | Full Trace
app/views/shared/_errors.html.erb:1:in `_app_views_shared__errors_html_erb__3874437342468649522_70028100096200'
app/views/users/_form.html.erb:1:in `_app_views_users__form_html_erb__2653828291511096048_70028100213300'
app/views/users/edit.html.erb:4:in `_app_views_users_edit_html_erb___925607472456440074_70028151623400'
Request
Parameters:
{"id"=>"2"}
Toggle session dump
Toggle env dump
Response
Headers:
None
这是我的用户控制器:
class UsersController < ApplicationController
def new
@user= User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the alpha blog #{@user.username}"
redirect_to articles_path
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:username, :email, :password)
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:success] = "Your account was updated successfully"
redirect_to articles_path
else
render 'edit'
end
end
end
由于我在编辑类中初始化了变量@user,我认为这样可行。
答案 0 :(得分:2)
在文件_app / views / users / form.html.erb 中,在呈现_shared / errors.html.erb 部分时,也将public function get_location_departments($d)
{
$this->db->where_in('location_id',$d);
$query=$this->db->get('department')->result();
return $query;
}
传递为本地的。这就是你如何做到的:
obj
如果它不起作用,请发布用户/编辑和用户/ _form 模板。
答案 1 :(得分:0)
以下是您的问题的2个答案
如果您正在使用&#34; 设计&#34; gem,尝试使用&#34; resource.errors &#34;而不是obj.errors
如果您要制作自定义表单,那么基本上在部分中,找不到对象。因此,无论此部分呈现在何处,您都可以尝试使用以下代码。所以你试图访问obj的错误,这是一个零类。尝试传递当地人如下
渲染(:partial =&gt;&#39; users / feeds&#39;,:locals =&gt; {:obj =&gt; @user})