我正在使用RoR Tutorial进行练习,并遇到一个奇怪的问题,即错误消息呈现两次。
new.html.slim
- provide(:title,'Sign up')
h1 Sign up
div class="row"
div class="col-sm-6 col-sm-offset-3"
= form_for(@user,html:{role:"form"}) do |f|
= render 'shared/error_messages'
div class="form-group"
= f.label :name
= f.text_field :name, class:"form-control"
div class="form-group"
= f.label :email
= f.text_field :email, class:"form-control"
div class="form-group"
= f.label :password
= f.password_field :password, class:"form-control"
div class="form-group"
= f.label :password_confirmation,"Confirmation"
= f.password_field :password_confirmation, class:"form-control"
= f.submit "Create Account", class: "btn btn-lg btn-primary"
在此代码(第7行)中,我呈现了部分shared/error_messages
。该页面的调用如下:
users_controller.rb
def create
@user = User.new(user_params)
if @user.save
else
render 'new'
end
end
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
有什么想法吗?
编辑:添加部分
= if @user.errors.any?
div#error_explanation
div class="alert alert-danger"
| The form has
= "#{@user.errors.count}"
| error(s)
ul
- @user.errors.full_messages.each do |msg|
li
= "#{msg}"
答案 0 :(得分:0)
我认为你的部分语法错误
= if @ user.errors.any?
到
- if @ user.errors.any?