Ruby form.html.haml未在new.html.haml上呈现
我是从《Rails 4下的敏捷Web开发》一书开始工作的,我在使用HAML代码时遇到了一些困难 我不太确定我做错了什么,但是当我开始构建新产品时,我的表单没有呈现出来。我检查了源代码,但它也不在HTML中,所以我的代码有问题,但不确定是什么。希望有人能帮助我 这是我的Form.html.haml代码Ruby form.html.haml未在new.html.haml上呈现,ruby,ruby-on-rails-3,ruby-on-rails-4,haml,Ruby,Ruby On Rails 3,Ruby On Rails 4,Haml,我是从《Rails 4下的敏捷Web开发》一书开始工作的,我在使用HAML代码时遇到了一些困难 我不太确定我做错了什么,但是当我开始构建新产品时,我的表单没有呈现出来。我检查了源代码,但它也不在HTML中,所以我的代码有问题,但不确定是什么。希望有人能帮助我 这是我的Form.html.haml代码 =if @product.errors.any? %div{ :id => "error_explanation" } %h2 =pluralize(@product.
=if @product.errors.any?
%div{ :id => "error_explanation" }
%h2
=pluralize(@product.errors.count, "error")
prohibited this product from being saved:
%ul
=@product.errors.full_messages.each do |msg|
%li
=msg
%div{ :class => "field" }
=f.label :title
=f.text_field :title
%div{ :class => "field" }
=f.label :description
=f.text_area :description, rows: 6
%div{ :class => "field" }
=f.label :image_url
=f.text_field :image_url
%div{ :class => "field" }
=f.label :price
=f.text_field :price
%div{ :class => "actions" }
=f.submit
%h1 New Product
=render 'form'
=link_to 'Back', products_path
这是我的新的.html.haml
=if @product.errors.any?
%div{ :id => "error_explanation" }
%h2
=pluralize(@product.errors.count, "error")
prohibited this product from being saved:
%ul
=@product.errors.full_messages.each do |msg|
%li
=msg
%div{ :class => "field" }
=f.label :title
=f.text_field :title
%div{ :class => "field" }
=f.label :description
=f.text_area :description, rows: 6
%div{ :class => "field" }
=f.label :image_url
=f.text_field :image_url
%div{ :class => "field" }
=f.label :price
=f.text_field :price
%div{ :class => "actions" }
=f.submit
%h1 New Product
=render 'form'
=link_to 'Back', products_path
提前谢谢。部分需要用
\uu
前缀命名
您的
Form.html.haml
必须被称为\u Form.html.haml
除了确保您的表单被命名为\u Form.html.haml
之外,您还需要修复haml中的一些嵌套。它应该是这样的:
=if @product.errors.any?
%div{ :id => "error_explanation" }
%h2
=pluralize(@product.errors.count, "error")
prohibited this product from being saved:
%ul
=@product.errors.full_messages.each do |msg|
%li
=msg
%div{ :class => "field" }
=f.label :title
=f.text_field :title
%div{ :class => "field" }
=f.label :description
=f.text_area :description, rows: 6
%div{ :class => "field" }
=f.label :image_url
=f.text_field :image_url
%div{ :class => "field" }
=f.label :price
=f.text_field :price
%div{ :class => "actions" }
=f.submit
您当前在表单字段上的缩进将其置于
if@product.errors.any?
块的范围内,这意味着只有@product
有错误时,表单才会出现。根据meagar和theTRON提供的答案以及您最后的评论:
您将在何处照亮窗体对象?好像什么地方都没有,所以你就犯了这个错误。当您通过form_for方法将表单绑定到模型对象时,它将生成一个form builder对象(f变量)
请尝试以下操作:
<%= form_for @product, url: {action: "create"} do |f| %>
# your code using f variable ...
<% end %>
#您的代码使用f变量。。。
让我们知道这是否最终修复了您的代码。FYI:您误用了HAML。编写
%div{:class=>“actions}”
基本上违背了HAML的目的。这应该简单地写。actions
,%div{:id=>“error\u explauration”}
应该是\error\u explauration
。那么@meagar,你是说%div{\error\u explauration}?对不起,我有点困惑。如何改进我的代码?不,整行代码都是\error\u explainion
。给你。使用HAML,您可以使用%tag.className
生成
,但是如果(并且仅当)标记是div
,您可以完全省略标记,只需使用.className
。同样的id
,它使用
而不是
,因此,%div(id=“what”)
,%div#what
或只是what
都是一样的,就像%li#what.big
和%li(id=“what”class=“big”)
一样。请注意,您的