开发者

a difference between an .erb and .haml in the form data

I have an .erb script, which works:

<% form_for :ticket do |f| %>
<% if @ticket.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ticket.errors.count, "error") %> prohibited this post from being  saved:</h2>
<ul>
<% @ticket.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p><b>department</b><br/>
<%= select("ticket", "department_id", Department.find(:all).collect{ |d| [ d.name, d.id] }) %>
</p>
<label for="ticket_subject">subject</label> <%= text_field "ticket", "subject" -%><br/>
<label for="ticket_request">request</label> <%= text_area "ticket", "message" -%>

<br/><br/><br/>
<%= submit_tag 'make a request' -%>
<% end %>

but when it was converted in haml:

- form_for :ticket do |f|
  - if @ticket.errors.any?
    #error_explanation
      %h2
        = pluralize(@ticket.errors.count, "error")
        prohibited this post from being saved:
      %ul
        - @ticket.errors.full_messages.each do |msg|
          %li= msg
  %label{:for => "ticket_name"} name
  = text_field "ticket", "name开发者_如何学编程"
  %br/
  %label{:for => "ticket_email"} email
  = text_field "ticket", "email"
  %br/
  %p
    %b department
    %br/
    = select("ticket", "department_id", Department.find(:all).collect{ |d| [ d.name, d.id] })
  %label{:for => "ticket_subject"} subject
  = text_field "ticket", "subject"
  %br/
  %label{:for => "ticket_request"} request
  = text_area "ticket", "message"
  %br/
  %br/
  %br/
  = submit_tag 'make a request'

I have such a mistake:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors

in this line:

- if @ticket.errors.any?

thanks


Please try

- form_for @ticket do |f|


It's not haml's fault, @ticket hasn't been defined. You should be defining it in your controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜