如何在用户中解决NoMethodError#edit in ror

时间:2015-01-09 05:21:49

标签: ruby ruby-on-rails-3 ruby-on-rails-4

请有人帮我解决以下错误。实际上我想在编辑页面的文本字段中显示数据库中出现的退出值。但是我收到了一些错误。

错误:

NoMethodError in Users#edit
Showing C:/Site/edit/app/views/users/edit.html.erb where line #4 raised:

undefined method `each' for #<User:0x3ac9ea8> 

以下是我的代码段

视图/用户/ 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) %>&nbsp;&nbsp;||&nbsp;&nbsp;<%= link_to "Reset Password",users_reset_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>

视图/用户/ edit.html.erb

<h1>Edit your data here</h1>
<center>
  <%= form_for @user ,:url => {:action => "update",:id => params[:id]} do |f| %>
      <% @edit.each do |t| %>
      <div class="div_reg">
        <p>
          <label for="username" class="uname" data-icon="u" >username </label>&nbsp;
          <%= f.text_field:name, :input_html => {:value => t.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,:input_html => {:value => t.email} %>
        </p>
        <p>
          <label for="username" class="uname" data-icon="u" >Password </label>&nbsp;&nbsp;
          <%= f.password_field:password,:input_html => {:value => t.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 %>
  <% 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
    @edit=User.find(params[:id])
  end
  def update
    flash[:notice]=params[:id]
    @user=User.find(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
  def reset
  @user=User.new
  end
  def emailsend
    @user=User.find(params[:id])
    if @user.email== params[:user][:email]
      UserMailer.registration_confirmation(@user).deliver
      flash[:notice]="Check your email to reset the password"
      flash[:color]="valid"
      redirect_to :action => 'reset'
    else
      flash[:notice]="Check your valid email or your email is not found"
      flash[:color]="invalid"
      redirect_to :action => 'show'
    end
  end
  def resetpass
  @user=User.new
  end
  def passres
    @user=User.find_by_email(params[:user][:email])
    if @user.update_attributes(updates_password)
      flash[:notice]="Your password id updated succefully"
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:alert]="Your data could not update..Please check it..!!"
      flash[:color]="invalid"
      redirect_to :action => 'show'
  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
  def updates_password
    params.require(:user).permit(:email,:password,:password_confirmation)
  end
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

请帮我解决此错误。谢谢。

1 个答案:

答案 0 :(得分:0)

@edit.each是你的问题。您的用户实例不是ActiveRecord用户数组,因此.each无法使用。

您可以移除迭代<% @edit.each do |t| %>并将所有引用t更改为@edit,即。 @edit.name