【问题标题】:Simple form in rails doesn't display validation error messagesrails 中的简单表单不显示验证错误消息
【发布时间】:2015-05-06 14:10:17
【问题描述】:

我的应用中有这个simple_form,但它没有显示任何验证错误消息:

<%= simple_form_for(@user) do |f| %>

     <%= f.error_notification %>

    <%= f.input :first_name, label: "Prénom" %>
    <%= f.input :last_name, label: "Nom" %>
    <%= f.input :email, label:"email" %>
    <%= f.input :telephone, label: "telephone"%>

    <p><%= f.label :birthdate, 'Date de naissance' %></p>
    <%= f.date_select :birthdate, {:include_blank => true, :default => nil, :use_month_names => ['Janv.','Fevr.', 'Mars', 'Avr.', 'Mai', 'Juin', 'Juil.', 'Août','Sept.', 'Oct.', 'Nov.', 'Déc.'], :order => [:day, :month, :year], :start_year => 1910, :end_year => 1995} %>
     <%= f.input :genre, label: "Sexe" %>
    <%= f.input :ranking, label:"Classement" %>
    <%= f.input :licence_number, label: "numéro de licence"%>
    <p>
      <%= f.label :Photo%>
      <%= f.file_field :picture %>
    </p>
    <p>
    <%= f.label :licence %>
    <%= f.file_field :licencepicture %>
    </p>
    <p>
    <%= f.label :certificat %>
    <%= f.file_field :certifmedpicture %>
    </p>
    <div id="validation"><%= f.submit %></div>
    <% end %>

这是我在用户模型中的验证

 validates :first_name, presence: { strict: true }, on: :update
 validates :last_name, presence: { strict: true }, on: :update

你可以看看我的simple_form.en.yml:

en:
simple_form:
"yes": 'Yes'
"no": 'No'
required:
  text: 'required'
  mark: '*'
  # You can uncomment the line below if you need to overwrite the whole required html.
  # When using html, text and mark won't be used.
  # html: '<abbr title="required">*</abbr>'
error_notification:
  default_message: "Certains champs posent problèmes:"

我的update 方法:

def update
  @user.update(user_params)
  @user.create_mangopay_natural_user_and_wallet
  redirect_to user_path(current_user)
end

我不明白为什么不显示错误通知,因为我认为f.error_notification 是正确的

【问题讨论】:

  • 您是否在控制器中正确检查错误?
  • 试试&lt;%= simple_form_for(@user), :validate =&gt; true do |f| %&gt;
  • @sjagr 我认为这是 simple_form 原生的。我该怎么做?这是我的更新方法:def update @user.update(user_params) @user.create_mangopay_natural_user_and_wallet redirect_to user_path(current_user) end
  • @sprungl 假设 OP 已经安装了client_side_validations-simple_form,对吧?

标签: ruby-on-rails validation simple-form


【解决方案1】:

正如您的 cmets 中所揭示的,您的控制器不会检查是否成功保存,只是盲目地重定向到用户页面。我没有看到来自simple_form 的任何本机功能可以为您执行此操作。您需要检查此成功保存并在失败时重新呈现编辑表单,以便它可以在f.error_notification 中显示这些错误:

def update
  if @user.update(user_params)
    @user.create_mangopay_natural_user_and_wallet
    redirect_to user_path(current_user)
  else
    render 'edit'
  end
end

【讨论】:

  • 对不起,但它仍然不起作用...当我尝试提交没有名字的表单时,我在 /users/5 处得到 ActiveModel::StrictValidationFailed 它不显示错误消息(其他部分是未执行)
猜你喜欢
  • 1970-01-01
  • 2022-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
  • 2022-07-07
  • 2016-03-05
  • 1970-01-01
相关资源
最近更新 更多