Search code examples
ruby-on-railshamlpartial-viewslocal-variablesrenderpartial

Haml Rails partial and locals issue


I just started playing around with haml and I am trying to understand what I am doing wrong.

articles/edit.html.haml

=render partial: 'form', f: f
  .submit_field
    =f.submit "Update Article"

articles/_form.html.haml

=form_for @article do |f|
  -if @article.errors.any?
  #error_explanation
    %h2
      =pluralize(@article.errors.count, "error")
      prohibited this task from being saved:
      %ul
        -@article.errors.full_messages.each do |msg|
          %li=msg

  .text_field
    =f.label :title 
    %br
    =f.text_field :title

  .text_field
    =f.label :body
    %br
    =f.text_area :body, {rows: 10, cols: 40}

I get this error: syntax error, unexpected keyword_ensure, expecting $end at .submit_field . Can anybody point me in the right direction?


Solution

  • Try moving the submit button in to your form partial:

    articles/edit.html.haml

    =form_for @article do |f|
      =render partial: 'form', f: f
    

    articles/_form.html.haml

    -if @article.errors.any?
    #error_explanation
      %h2
        =pluralize(@article.errors.count, "error")
        prohibited this task from being saved:
        %ul
          -@article.errors.full_messages.each do |msg|
            %li=msg
    
    .text_field
      =f.label :title 
      %br
      =f.text_field :title
    
    .text_field
      =f.label :body
      %br
      =f.text_area :body, {rows: 10, cols: 40}
    
    .submit_field
      =f.submit "Update Article"