客户无法注册我的网络应用程序。在用户#new
中获取错误NameError错误消息。显示/Users/neilpatel/Desktop/Rails/sample_app/app/views/shared/_error_messages.html.erb,其中第1行被提出:
**<% if object.errors.any? %> <-error**
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
这是他们应该看到的添加注册详情的视图
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
这是新注册的代码
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= render 'fields', f: f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
这是新用户确认码
<li>
<%=link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
跟踪路线
Started GET "/signup" for 127.0.0.1 at 2014-02-10 20:56:41 +0000
Processing by UsersController#new as HTML
Rendered shared/_error_messages.html.erb (0.1ms)
Rendered shared/_error_messages.html.erb (43.9ms)
Rendered users/_fields.html.erb (44.8ms)
Rendered users/new.html.erb within layouts/application (47.9ms)
Completed 500 Internal Server Error in 51ms
ActionView::Template::Error (undefined local variable or method `object' for #<#<Class:0x007fe9aa752d58>:0x007fe9a9fb3b88>):
1: <% if object.errors.any? %>
2: <div id="error_explanation">
3: <div class="alert alert-error">
4: The form contains <%= pluralize(object.errors.count, "error") %>.
app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb__4173887643235544536_70320816602040'
app/views/users/_fields.html.erb:1:in `_app_views_users__fields_html_erb__1761016343315845667_70320816564760'
app/views/users/new.html.erb:8:in `block in _app_views_users_new_html_erb___922995521922748898_70320816483500'
app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___922995521922748898_70320816483500'
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (16.1ms)
user.controller
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update, :destroy, :following]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [:destroy, :following, :followers]
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end
def new
@user = User.new
end
def edit
#@user = User.find(params[:id])
end
def update
#@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user # Handle a successful update.
else
render 'edit'
end
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
答案 0 :(得分:0)
在第一个表单中,您需要将对象分配给object
:
<%= render 'shared/error_messages', object: @user %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
您实际上是在代码中的其他地方正确地执行此操作,看起来像是一个简单的疏忽。