ror4格式中的第一个参数不能包含nil或为空 [英] ror4 First argument in form cannot contain nil or be empty

查看:329
本文介绍了ror4格式中的第一个参数不能包含nil或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击编辑时,我得到:
表单中的第一个参数不能包含nil或在bets / app / views / users / _form.html.erb中为空

When I click the edit I get: First argument in form cannot contain nil or be empty in bets/app/views/users/_form.html.erb

users / _form

users/_form

<%= form_for(@user) do |f| %>

  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %>
      prohibited this user from being saved:</h2>

      <ul>
        <% @user.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= f.label :name, "Username" %><br />
    <%= f.text_field :name %>
  </p>

  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>

  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>

  <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </p>
<% if @user.new_record? %>
<%= f.submit "Sign up" %>
<% else %>
<%= f.submit "Update Profile" %>
<% end %>
<% end %>

在users_controller中

in users_controller

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
    flash[:notice] = "You have signed up successfully."
    redirect_to posts_path
    else
    render :new
    end
  end

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

 def update
    if @user.update(user_params)
      flash[:notice] = "Profile has been updated"
      redirect_to @user 
    else
      flash[:alert] = "Profile has not been updated"
      render 'edit'
    end
  end
  def edit
  end

  def destroy
        @user = User.find(params[:id])
        @user.destroy
        flash[:notice] = "Post has been destroyed."
        redirect_to posts_path
    end

  private
def user_params
params.require(:user).permit(:name,:password,:password_confirmation, :email)
end

end

在模型用户中

class User < ActiveRecord::Base
    has_secure_password

end

编辑

<h2>Edit Profile</h2>
<%= render 'form' %>

视图/显示

<h1>Profile Page for <%= @user.name %></h1>
<p>Email: <%= @user.email %></p>
<p><%= link_to "Edit Profile", edit_user_path(@user) %></p>
<%= link_to "Delete Profile",user_path(@user),method: :delete,
data: { confirm:"Are you sure you want to delete this post?"} %>

问题出在哪里,如何解决?
ps:是rails 4

What is the problem and how can I fix it? p.s.: is rails 4

推荐答案

您应设置 @user edit 操作中的实例变量:

You should set @user instance variable in edit action:

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

原因您遇到此错误是因为您将 @user 实例变量作为 form_for 的参数传递。该变量的值为 nil ,因为您没有在 edit 操作中进行设置。

The reason you have this error is you pass @user instance variable as argument to form_for . This variable evaluates to nil, since you don't set it in your edit action.

顺便说一句,您在 update 动作中也遇到了类似的错误-我很确定它会崩溃(带有 undefined如果调用它,则方法 update nil )。您应该插入行

BTW, you have similar bug in update action - I'm quite sure it will crash (with "undefined method update for nil") if you call it. You should insert line

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

update 操作的顶部。

这篇关于ror4格式中的第一个参数不能包含nil或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆