'错误'部分适用于'新'用户,但不适用于'编辑'用户

时间:2018-05-31 06:10:32

标签: ruby-on-rails

我正在学习Ruby on Rails和100%的新编码。 目前,我收到此消息:

  

提取的源(第1行):nil的未定义方法`errors':NilClass

<% if obj.errors.any? %>

我只在edit.html.erb中收到此消息:

<%= render 'shared/errors', obj: @user %>

<div class='row'>
    <div class='col-xs-12'>
        <%= form_for(@user, :html => {class: "form-horizontal", role: "form"}) do |f| %>
            <div class="form-group">
                <div class="control-label col-sm-2">
                    <%= f.label :username %>
                </div>
                <div class="col-sm-8">
                <%= f.text_field :username, class: "form-control", placeholder: "Enter Username", autofocus: true %>
                </div>
            </div>
            <div class="form-group">
                <div class="control-label col-sm-2">
                    <%= f.label :email %>
                </div>
                <div class="col-sm-8">
                    <%= f.email_field :email, class: "form-control", placeholder: "Enter Email"  %>
                </div>
            </div>
                  <div class="form-group">
                <div class="control-label col-sm-2">
                    <%= f.label :password %>
                </div>
                <div class="col-sm-8">
                    <%= f.password_field :password, class: "form-control", placeholder: "Enter Password"  %>
                </div>
            </div>
            <div class = "form-group">
                <div class = "col-sm-offset-2 col-sm-10">
                    <%= f.submit 'Sign Up', class: "btn btn-primary btn-md" %>
                </div>
            </div>
        <% end %>

        <div class = "col-xs-4 col-xs-offset-5">
            [ <%= link_to 'Back to Index', articles_path %> ]
        </div>
</div>
    </div>

我的new.html.erb具有完全相同的代码,但没有收到错误消息。这是UsersController:

class UsersController < ApplicationController

   def new 
      @user = User.new 
   end

   def create
       @user = User.new(user_params)
       if @user.save
           flash[:success] = "Welcome to Alpha Blog #{@user.username}"
           redirect_to articles_path
       else
           render 'new'
        end

    def edit
            @user = User.find(params[:id])
        end

    def update

    end
   end


    private

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

这是错误部分:

<% 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">
                    <%= pluralize(obj.errors.count, "error") %>
                    prohibited this article from being saved:
                    </h2>
                    <div class ="panel-body">
                        <ul>
                             <% obj.errors.full_messages.each do |msg| %>
                                <li><%= msg %></li>
                             <% end %>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
<% end %>

我做错了什么?提前谢谢!

3 个答案:

答案 0 :(得分:1)

当有大量的宝石和助手为你做这件事时,不确定为什么你会这样做。

问题是,您忘记了localspartial,因为您可以在Rails guide中阅读。

<%= render partial: 'shared/errors', locals: {obj: @user} %>

就我个人而言,我建议将obj更改为object,我们希望在栏目中写出来。

答案 1 :(得分:1)

您的update操作似乎为空我的意思是没有任何有效的操作代码,您可以添加一些有效的代码进行更新,如下所示

def update
    @user = User.find(params[:id])
    respond_to do |format|
        if @user.update_attributes(user_params)
            flash[:success] = 'User was successfully updated.'
            format.html { redirect_to @user }
            format.xml  { head :ok }
        else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
    end
end 

现在尝试使用不正确的数据检查验证。

答案 2 :(得分:0)

事实证明我关闭了&#34;如果&#34;在创建方法中阻止,但没有关闭创建方法本身。