如何解决未定义的方法`update_attributes'为nil:NilClass错误

时间:2015-01-03 10:50:20

标签: ruby-on-rails sqlite

任何人都可以帮我解决这个错误。当我更新我的数据库值时,我收到以下错误。

错误: 未定义的方法`update_attributes'为零:NilClass

我的代码片段如下所示。

视图/用户/ index.html.erb:

<h1>This is index page</h1>
<center>
<p>Enter data</p>
 <div class="option">
   <p><%= link_to "Click here to enter data",users_new_path %></p>
   <p><%= link_to "Display data",users_show_path%></p>
 </div>

 </center>

视图/用户/ show.html.erb

<h1>Show your data</h1>
<center>
  <ul>
    <% @user.each do |t| %>
    <li>
      <%= t.name %> |
     <%= t.email %> |
      <%= t.password%> |
      <%= t.created_at %>
      <%= link_to "edit",users_edit_path(:id => t.id) %>
    </li>
        <% end %>
  </ul>
  <div class="back_btn">
    <a href="/users/index"><button type="button" class="btn-custom " style="cursor:pointer;">Back</button></a>
  </div>
</center>

视图/用户/ new.html.erb:

<h1>Enter your data here</h1>
<center>
  <%= form_for @user ,:url => {:action => "create"} do |f| %>
  <div class="div_reg">
    <p>
      <label for="username" class="uname" data-icon="u" >username </label>&nbsp;
      <%= f.text_field:name,placeholder:"Enter your user name" %>
    </p>
    <p>
      <label for="username" class="uname" data-icon="u" >Email </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <%= f.text_field:email,placeholder:"enter your email" %>
    </p>
    <p>
      <label for="username" class="uname" data-icon="u" >Password </label>&nbsp;&nbsp;
      <%= f.password_field:password,placeholder:"Enteryour password" %>
    </p>
    <p>
      <label for="username" class="uname" data-icon="u" >Password </label>&nbsp;&nbsp;
      <%= f.password_field  :password_confirmation %>
    </p>
    <center>
      <%= f.submit "Submit",:class => 'btn-custom' %>
    </center>
    <div class="back_btn">
      <a href="/users/index"><button type="button" class="btn-custom " style="cursor:pointer;">Back</button></a>
    </div>
  </div>
      <% end %>
</center>
<% if @user.errors.any? %>
    <ul class="Signup_Errors">
      <% for message_error in @user.errors.full_messages %>
          <li><%= message_error %></li>
      <% end %>
    </ul>
<% end %>

视图/用户/ edit.html.erb:

<h1>Edit your data here</h1>
<center>
  <%= form_for @user ,:url => {:action => "update"} do |f| %>
      <div class="div_reg">
        <p>
          <label for="username" class="uname" data-icon="u" >username </label>&nbsp;
          <%= f.text_field:name,placeholder:"Enter your user name" %>
        </p>
        <p>
          <label for="username" class="uname" data-icon="u" >Email </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <%= f.text_field:email,placeholder:"enter your email" %>
        </p>
        <p>
          <label for="username" class="uname" data-icon="u" >Password </label>&nbsp;&nbsp;
          <%= f.password_field:password,placeholder:"Enteryour password" %>
        </p>
        <p>
          <label for="username" class="uname" data-icon="u" >Password </label>&nbsp;&nbsp;
          <%= f.password_field  :password_confirmation %>
        </p>
        <center>
          <%= f.submit "Update",:class => 'btn-custom' %>
        </center>
        <div class="back_btn">
          <a href="/users/index"><button type="button" class="btn-custom " style="cursor:pointer;">Back</button></a>
        </div>
      </div>
  <% end %>
</center>
<% if @user.errors.any? %>
    <ul class="Signup_Errors">
      <% for message_error in @user.errors.full_messages %>
          <li><%= message_error %></li>
      <% end %>
    </ul>
<% end %>

控制器/ users_controller.rb:

class UsersController < ApplicationController
  def index

  end
  def new
@user=User.new
  end
  def create
@user=User.new(users_param);
    if @user.save
 flash[:notice]="You signed up successfully"
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:alert]="You have not signed up successfully"
      flash[:color]="invalid"
      redirect_to :action => 'new'
    end
  end
  def show
    @user=User.all
  end
  def edit
    @user=User.new
  end
  def update
    @user=User.find_by_id(params[:id])
    if @user.update_attributes(update_params)
      flash[:notice]="Your data is updated succesfully"
      flash[:color]="valid"
      redirect_to :action => 'show'
    else
      flash[:alert]="Your data could not update,Please check it..!!"
      flash[:color]="invalid"
      redirect_to :action => 'edit'
    end
  end
  private
  def users_param
    params.require(:user).permit(:name, :email, :password,:password_confirmation)
  end
  def update_params
    params.require(:user).permit(:name,:email,:password,:password_confirmation)
  end
end

配置/ routes.rb中:

Rails.application.routes.draw do
  root "users#index"
  get "users/new" => "users#new"
  get "users/index" => "users#index"
  post "users/create" => "users#create"
  get "users/show" => "users#show"
  get "users/edit" => "users#edit"
  post "users/update" => "users#update"
end

模型/ user.rb:

class User < ActiveRecord::Base
  #attr_accessor :password
  #attr_accessor :name
  #attr_accessor :email
  EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :confirmation => true
  validates_length_of :password, :in => 6..20, :on => :create
end

请帮我解决给定的错误。我使用的是rails version-4和ru​​by 1.9.3。

2 个答案:

答案 0 :(得分:2)

路线错误,他们没有考虑:id参数。因此,操作无法加载相应的用户。

上面的代码中有几个错误。它是一个相当简单的脚手架,您应该使用rails g scaffold并生成一个脚手架,以便您可以学习如何正确使用Rails约定。

可以删除所有这些路线

get "users/new" => "users#new"
get "users/index" => "users#index"
post "users/create" => "users#create"
get "users/show" => "users#show"
get "users/edit" => "users#edit"
post "users/update" => "users#update"

并替换为资源

resources :users

编辑应加载现有资源,而不是创建新资源。

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

同样适用于更新。使用find,以便在找不到资源时引发它。

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

同样,还有很多需要完成的清理工作。我建议你开始从默认的脚手架中读取代码并相应地修改你的代码。

答案 1 :(得分:0)

我已经清理了你的控制器和routes.rb。看看是否有帮助 控制器/ users_controller.rb:

class UsersController < ApplicationController
  def index
    @user = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(users_params)
    if @user.save
      flash[:notice] = 'You signed up successfully'
      flash[:color] = 'valid'
      redirect_to action: 'index'
    else
      flash[:alert] = 'You have not signed up successfully'
      flash[:color] = 'invalid'
      redirect_to action: 'new'
    end
  end

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

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

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(users_params)
      flash[:notice] = 'Your data is updated succesfully'
      flash[:color] = 'valid'
      redirect_to action: 'show'
    else
      flash[:alert] = 'Your data could not update,Please check it..!!'
      flash[:color] = 'invalid'
      redirect_to action: 'edit'
    end
  end

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

配置/ routes.rb中:

Rails.application.routes.draw do
  root 'users#index'
  resources: :users
end