我正在关注Rails教程,我无法让我的_error_messages.html.erb
部分渲染。单击“提交”时,它应该在注册页面上显示错误,并且表单为空。相反,我得到:
partial users/user with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/Users/hmumin/rails_projects/sample_app/app/views"
我不明白为什么它会搜索部分用户,因为它应该搜索_error_message.html.erb
部分用户。
我的用户控制器是:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params) #not the final implemention!
if @user.save
#handle a successful save
else
render new
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
注册页面是:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= 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, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
我的error_messages部分是:
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
答案 0 :(得分:0)
确保文件_error_messages.html.erb位于正确的文件夹中。这是:
应用程序/视图/共享