Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 编辑页面的未定义方法_Ruby On Rails - Fatal编程技术网

Ruby on rails 编辑页面的未定义方法

Ruby on rails 编辑页面的未定义方法,ruby-on-rails,Ruby On Rails,我不熟悉Rails。我的新.html.erb文件在http://localhost:3000/signup。然而,我似乎无法工作。我收到这个错误: undefined method `model_name' for NilClass:Class Extracted source (around line #3): 1: <h1>Account Information</h1> 2: 3: <%= form_for @user do |f| %> 4:

我不熟悉Rails。我的新.html.erb文件在
http://localhost:3000/signup
。然而,我似乎无法工作。我收到这个错误:

undefined method `model_name' for NilClass:Class
Extracted source (around line #3):

1: <h1>Account Information</h1>
2: 
3: <%= form_for @user do |f| %>
4:   <% if @user.errors.any? %>
5:     <div class="error_messages">
6:       <h2>Form is invalid</h2>
NilClass:Class的未定义方法“model_name” 提取的源(第3行附近): 1:帐户信息 2: 三: 4: 5: 6:表格无效 这是我的edit.html文件,它是new.html的一个副本。我试图删除错误消息代码,但它仍然只是在页面上显示另一个错误

<h1>Account Information</h1>

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

    <div class="field">
        <%= f.label :email %><br/>
        <%= f.text_field :email %>
    </div>
    <div class="field">
        <%= f.label :password %><br/>
        <%= f.password_field :password %>
    </div>
    <div class="field">
        <%= f.label :password_confirmation %><br/>
        <%= f.password_field :password_confirmation %>
    </div>
    <div class="field">
        <%= f.label :username %><br/>
        <%= f.text_field :username %>
    </div>
    <div class="field">
        <%= f.label :zip_code %><br/>
        <%= f.text_field :zip_code %>
    </div>
    <div class="field">
        <%= f.label :birthday %><br/>
        <%= f.text_field :birthday %>
    </div>  
    <div class="actions"><%= f.submit %></div>
<% end %>
账户信息
表格无效






这是我的用户\控制器,我不确定您是否需要查看。也许我把def编辑部分弄错了

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

  def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      session[:user_id] = @user.id
      redirect_to root_url, notice: "Thank you for signing up!"
    else
      render "new"
    end

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

  def update
    @user = User.find(params[:user])
    if @user.update_attributes(params[:user])
      flash[:success] = "Account updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
  end
end
end
end
class UsersController
您的代码缩进在这里是一个信号;您正在定义
def create
中的
edit
update
方法;紧接着之前的
结束
关闭
if@user.save
,而不是
def create

您的
@user
为零;你在传递一个有效的用户ID吗?这就是我现在试图弄明白的,多亏了Chowlett的回答,因为它导致了现在显示“找不到没有ID的用户”的错误。所以现在我只想让它显示有效的身份证。谢谢。这将我推向一个新的错误..但这是一个进步,因为现在我知道我需要找出一个有效的用户ID。