新手问题 slim,把 erb 改成 slim 之后,表单提交报错。

1272729223 · 2012年08月08日 · 最后由 1272729223 回复于 2012年08月08日 · 2831 次阅读

应该是 slim 写错的问题

= form_for(@blog) do |f|
  - if @blog.errors.any?
    #error_explanation
      h2 = pluralize(@blog.errors.count,"error")
      ul
        = @blog.errors.full_messages.each do |msg|
          li 
            = msg

  .field
    label for="title" Title
    br
    input type="text" name="title"
  .field
    label for="content" Content
    br
    textarea name="content"

  .actions
    input type="submit" value="POST IT!"

我添加标题和内容之后提交,还是显示 errors 并且输出的 html 有错误

<div id="error_explanation">
<h2>2 errors</h2>
<ul>
<li>Content can't be blank</li>
<li>Title can't be blank</li>
["Content can't be blank", "Title can't be blank"]
</ul>
</div>
      h2 = pluralize(@blog.errors.count,"error")
      ul
        - @blog.errors.full_messages.each do |msg|
          li 
            = msg```

这里弄错了,是一个` - @blog.errors.full_messages.each do |msg|`
输出正常,但是还是提交报错

因为你是自己写 input 和 textarea 标签,name 对不上。用

f.text_field :title
f.text_area :content

好的 谢谢你

= form_for(@blog) do |f|
  - if @blog.errors.any?
    #error_explanation
      h2 = pluralize(@blog.errors.count,"errors")
      ul
        - @blog.errors.full_messages.each do |msg|
          li = msg

  .field
    label for="title" Title
    br
    = f.text_field :title
  .field
    label for="content" Content
    br
    = f.text_area :content

  .actions
    input type="submit" value="POST IT!"

这样 ok 了,还有换成f.text_field :title这样前面加上=号就好了 = f.text_field :title

需要 登录 后方可回复, 如果你还没有账号请 注册新账号