Rails 4.0.2 haml版本的form_for让我发疯了

时间:2014-02-26 19:57:46

标签: syntax ruby-on-rails-4 haml

我对Rails世界很陌生,我正在尝试为票证模型创建表单

我在 routes.rb

中有以下内容
resources :tickets

在我的 ticket_controller 中,我有以下

def new
    @ticket = Ticket.new
end

在我的 tickets / new.html.haml

= form_for(@ticket, :url => { :action => "create" }) do |f| # <- this line is causing error
...

我的错误是语法错误,意外')'

我不明白原因是类似的模板文件正在工作。此外,如果我尝试编写以下 ticket / new.html.erb ,则表明它正在运行

<%= form_for(:ticket, :url => { :action => "create" }) do |f| %>

<% end %>

请你帮我理解我到底做错了什么?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我发现了你的问题,一个与缩进有关(if-block),另一个是额外的逗号(提交按钮)。

%h1{:class => ["page-header"]} New Ticket
= form_for(@ticket, :url => { :action => "create" }) do |f|
    = if @ticket.errors.any?
        %h2 #{pluralize(@ticket.errors.count, "error")} + " prohibited this post from being saved:"  ### code indentation done as it belongs to if block
        %ul
            = @ticket.errors.full_messages.each do |msg|
                %li = msg
    %p
        = f.label :subject
        = f.text_field :subject
    %p
        = f.label :body
        = f.text_area :body
    %p
        = f.submit :class => "btn btn-primary"  ### Removed comma

= link_to 'Back', tickets_path