无法获取nested_form来显示验证错误

问题描述 投票:0回答:1

我之前没有遇到验证问题,但这次我遇到了nested_form验证问题。我正在使用Twitter Bootstrap并且可以显示闪存错误,例如:

 def create
 @recipe = current_user.recipes.new(params[:recipe])
 if @recipe.save
  redirect_to my_recipes_path, :notice => "Thanks #{current_user.name} Recipe sucessfully created."
  else
  render :action => 'new'
 end
end

对于我的flash消息,我在我的app / layouts中使用它

 <% flash.each do |name, msg| %>
 <div class="alert alert-<%= name == :notice ? "success" : "error" %>">
  <a class="close" data-dismiss="alert">×</a>
  <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
 </div>
 <% end %>

所以我想我会尝试让其中一个验证器工作,所以我的模型

class Recipe < ActiveRecord::Base

belongs_to :user
delegate :name, :to => :user, :prefix => :user, :allow_nil => true
belongs_to :country
has_many :ingredients 
has_many :preperations
has_many :favourites

validates_presence_of:dish_name

和我的形式

    <%= nested_form_for @recipe  do |f| %>

    <div class="field_with_errors"> 
    <%= f.label :dish_name, "Dish Name" %>
    <%= f.text_field :dish_name, :placeholder => "Enter Dish Name" %>
    </div>


    <%= f.label :country_id, "Country Of Origin" %>
    <%= f.collection_select(:country_id, Country.all, :id, :name, :prompt => 'Please select country') %>

    <%= f.label :category, "Category" %>
    <%= f.select :category, [['Starter'], ['Main Course'], ['Desserts'],  ['Vegeterian']], {:include_blank => 'Please Select'} %>


   <%= f.label :difficulty, "Difficulty Level" %>
   <%= f.select :difficulty, [['Beginner'],['Intermediate'],['Expert']], {:include_blank => 'Please Select'} %>

   <%= f.label :preperation_time, "Preperation Time (Mins)" %>
   <%= f.select :preperation_time, [['15-30 Mins'],['30-60 Mins'],['60-120 Mins']], {:include_blank => 'Please Select'} %>

   <%= f.fields_for :ingredients do |ing| %>
   Ingredient<br>
   <%= ing.text_field :ingredient_name , :placeholder => "Enter Ingredient Here" %><br>
   <% end %>
   <%= f.link_to_add "Add an Ingredient", :ingredients %><br>

   <%= f.fields_for :preperations do |prep| %>
   Preperation Step<br>
   <%= prep.text_field :prep_steps , :placeholder => "Enter step Here" %><br>
   <% end %>

   <%= f.link_to_add "Add a step", :preperations %><br>

   <%= f.label :description, "Description of Recipe" %>
   <%= f.text_area :description, :size=> "60x10" %></br>

   <%= f.file_field :avatar %><br>

   <%= f.submit "Submit Recipe" %>
   <% end %>

我对Rails相当新,所以我可能错过了一些基本的东西,或者是因为它是一个嵌套的形式而且它的行为有所不同?

编辑

<%= flash debug %>的输出:

  --- !ruby/object:ActionDispatch::Flash::FlashHash
 used: !ruby/object:Set
 hash: {}
 closed: false
 flashes: {}
 now:
ruby-on-rails ruby-on-rails-3 validation nested-forms
1个回答
2
投票

看起来你实际上并没有提供你的flash哈希与任何消息。快速解决方案可能是这样的:

def create
 @recipe = current_user.recipes.new(params[:recipe])
 if @recipe.save
   redirect_to my_recipes_path, :notice => "Thanks #{current_user.name} Recipe sucessfully created."
 else
   flash[:error] =  @recipe.errors.full_messages.to_sentence
   render :action => 'new'
 end
end
© www.soinside.com 2019 - 2024. All rights reserved.