gpt4 book ai didi

ruby-on-rails - Ruby on Rails 中的 Object.errors 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:28 25 4
gpt4 key购买 nike

我有一个在 Users#new 中引发 NoMethodError 的表单错误部分。它说“nil:NilClass 的未定义方法 errors

这是我通过以下方式调用的部分:<%= render 'shared/error_messages', object: f.object %>

<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

new.html.erb对于 users :

<%= simple_form_for :user, url: register_path do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :username, label: false, placeholder: "Username (maximum: 12 characters)", class: "form-control" %>
<%= f.input :first_last_name, label: false, placeholder: "First & Last Name", class: "form-control" %>
<%= f.input :email, label: false, placeholder: "Email Address", class: "form-control" %>
<%= f.input :password, label: false, placeholder: "Create a Password (minimum: 6 characters)", class: "form-control" %>
<%= f.button :submit, "Create an Account!", class: "submit btn btn-block btn-success" %>
<% end %>

users_controller :

before_action :logged_in_user, only: [:edit, :update, :show]
before_action :correct_user, only: [:edit, :update, :show]
before_action :admin_user, only: :destroy

def new
@title = 'Register';
@user = User.new;
end

def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end

private

def user_params
params.require(:user).permit(:username, :first_last_name, :email, :password)
end

# Before filters

# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end

# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end

我真的不明白为什么这行不通,感谢您的帮助!

最佳答案

将您打开表单的方式更改为此(而不是传递您在新操作中创建的对象的符号):

<%= simple_form_for @user, url: register_path do |f| %>

关于ruby-on-rails - Ruby on Rails 中的 Object.errors 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com