ruby-on-rails - ror4 表单中的第一个参数不能包含 nil 或为空

标签 ruby-on-rails ruby-on-rails-4 edit

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

用户/_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 %>

在用户 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' %>

在 View /显示中

<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?"} %>

问题是什么以及如何解决? p.s.:是rails 4

最佳答案

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

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

出现此错误的原因是您将 @user 实例变量作为参数传递给 form_for 。该变量的计算结果为 nil,因为您没有在 edit 操作中设置它。

顺便说一句,您在 update 操作中也有类似的错误 - 我很确定它会崩溃(使用“未定义方法update for nil” ”)如果你这么称呼的话。您应该插入行

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

位于更新操作的顶部。

关于ruby-on-rails - ror4 表单中的第一个参数不能包含 nil 或为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19809889/

相关文章:

ruby-on-rails - Sidekiq 在完成作业后不会释放内存

mysql - JTable 的编辑按钮,用于编辑数据库内容 生成

Java、Swing、获取和更改所有输入字段

ruby-on-rails - 检查具有特定参数的重新请求作业是否在队列中挂起

ruby-on-rails - 打开Spree::User类并添加功能而不丢失现有功能

jquery - 使用 InstantEdit 2.0 JS 内联编辑 mysql 数据

ruby-on-rails - 使用 Rails 的 asset pipeline 时如何获取实际文件路径(与 send_file 一起使用)

ruby-on-rails - 在 CDN 中管理 Assets

javascript - Algolia Instantsearch for rails 没有结果

ruby-on-rails - 在 Rails 中,我应该在 capybara (或集成)测试中包含用户输入表单错误流吗?