下拉列表错误消息未显示 [英] Drop Down List error Message not shown

查看:90
本文介绍了下拉列表错误消息未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有3个级联下拉菜单和一个Boat模型(brand_idyear_idmodel_id).错误未显示.我添加了;

So I have 3 cascading drop downs and a Boat Model (brand_id, year_id, model_id). The errors are not shown. I have add;

  validates :brand_id, presence: true
  validates :year_id, presence: true
  validates :model_id, presence: true
  validates :user_id, presence: true

#boat.rb.我有我的表格;

<%= form_for(@boat) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
      <div class="col-md-6">
      <%= f.label :Brand %>
      <%= f.collection_select(:brand_id,  @brands,  :id, :name, {:prompt   => "Select a Brand"}, {:id => 'brands_select'}) %>
    </div>
    <div class="col-md-6">
      <%= f.label :Year %>
      <%= f.collection_select(:year_id, @years, :id, :name, {:prompt   => "Select a Year"}, {:id => 'years_select'}) %>
       </div>
      <div class="col-md-6">
      <%= f.label :Model %>
      <%= f.collection_select(:model_id, @models, :id, :name, {:prompt   => "Select a Model"}, {:id => 'models_select'}) %>
     </div>
      <div class="col-md-6 col-md-offset-3">
      <%= f.submit "Next", class: "btn btn-primary"%>
     </div>
    <% end %>

我有shared/errors

<% if object.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(object.errors.count, "error") %>.
    </div>
    <ul>
    <% object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

该共享错误消息非常适合用户模型.还有我的#create动作

That shared error message works perfectly for user model. And my #create action

def create
   @boat = current_user.boats.build(boat_params) if logged_in?
    if @boat.save
      flash[:success] = "Boat created!"
      redirect_to root_url
    else
      redirect_to new_boat_path(current_user) #returns here
    end
  end

我呼叫new.html并打开表单.当我不选择任何内容而按时,它将重定向到new_boat_path(current_user)而不显示错误.我不知道为什么.

I call the new.html and form opens. When I press without selecting anything it redirects to new_boat_path(current_user) without showing errors. I dont know why.

这是日志:

Started GET "/boats/new.2" for 88.240.3.128 at 2015-04-16 21:58:01 +0000
Processing by BoatsController#new as 
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  Rendered shared/_error_messages.html.erb (0.0ms)

因此它呈现,但我在页面上看不到任何错误

So it renders but i do not see any errors on the page

#new方法(品牌,年份和型号是级联的下拉列表,其中Boat模型具有其ID)

EDIT 2: #new method (brand, year & model are cascading drop down lists, where Boat model has their ids)

def new
    @boat = Boat.new
    @brands  = Brand.all
    @years = Year.all
    @models   = Model.all
  end

推荐答案

您是redirectingnew_boat_path. new操作将创建一个新的Boat实例,该实例显然将不包含任何错误.

You are redirecting to new_boat_path. The new action creates a new instance of Boat which will obviously not contain any errors.

您可能想做的是:

if @boat.save
  flash[:success] = "Boat created!"
  redirect_to root_url
else
  render 'new'
end

这将不会执行重定向,而只会呈现new视图,该视图包含@boat变量,其中包括通过调用@boat.save创建的验证错误.

This will not perform a redirect but just renders the new view which contains the @boat variable including the validation errors created by calling @boat.save.

这篇关于下拉列表错误消息未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆