Ruby on rails Can';无法获取嵌套表单以显示验证错误
我以前没有遇到过验证方面的问题,但这次我遇到了嵌套表单验证方面的问题。我正在使用Twitter引导,可以通过以下方式显示flash错误:Ruby on rails Can';无法获取嵌套表单以显示验证错误,ruby-on-rails,ruby-on-rails-3,validation,nested-forms,Ruby On Rails,Ruby On Rails 3,Validation,Nested Forms,我以前没有遇到过验证方面的问题,但这次我遇到了嵌套表单验证方面的问题。我正在使用Twitter引导,可以通过以下方式显示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 create
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消息,我在我的应用程序/布局中使用它
<% 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 %>
看起来你实际上并没有为你的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
感谢您的回答,虽然没有任何更改,但仍然返回到新操作(表单),没有显示错误消息shownbrilliant,现在显示为引导错误:),为什么现在可以这样做,是因为它首先重新排序视图并将其视为一个全新的表单吗?这实际上是一个好问题,我提供的初始语法
render:action=>'new',:flash=>{..}
实际上是旧的rails 2.3风格的语法。据我所知,它被拿走了,最近才放回去。我最好的猜测是因为我们都在使用不同版本的Rails,它在3.2.8上对我有效。。您能否将行=debug flash
添加到视图代码中,并将输出(在您未能提交表单后)附加到您的答案中?此外,虽然与手头的问题没有直接关系,但如果您想利用Twitter引导支持的其他警报消息,您可能会发现将行
更改为
会更成功。否则,您只能获得成功或错误。抱歉,但以前从未使用过此选项,它在视图中的外观如何?感谢引导提示:)哦,对不起,这是haml代码。在
的右上角添加行
,该行已更新为添加到问题的输出
<%= 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 %>
--- !ruby/object:ActionDispatch::Flash::FlashHash
used: !ruby/object:Set
hash: {}
closed: false
flashes: {}
now:
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