views:

28

answers:

1

What is the correct way to use the form tag, I am getting a compile error when I include the end. When I take it out it works, should I just manually end the form with HTML? Or is there something wrong with my syntax?

   <html>
     <head>
      <title>
      Databse connections
      </title>
     </head>
     <body>
      <%= form_tag ( :action => 'create' )%>
       <%= text_field(:album, :title) %>
       <%= text_field(:album, :artist) %>
       <%= text_field(:album, :genre) %>
       <%= datetime_select(:album, :release_date) %>
       <%= submit_tag("Create") %>  
      <% end %>

     </body>
    </html>
+1  A: 

If you use form_tag without a block, it'll only create the opening tag. If you want to create both tags, you need to pass it a block, which you appear to be trying to do, but you are missing the do keyword after form_tag(...):

<% form_tag ( :action => 'create' ) do %>
  <%= text_field(:album, :title) %>
  <%= text_field(:album, :artist) %>
  <%= text_field(:album, :genre) %>
  <%= datetime_select(:album, :release_date) %>
  <%= submit_tag("Create") %>  
<% end %>

Without do to start the block, the end is a syntax error. Without the end in your current syntax, you are not specifying that the fields are to be within the form (but they will end up being part of your form because you aren't closing your form tag created by the block-less form_tag before specifying them).

Daniel Vandersluis
Also, he was using `<%= form_tag` rather than `<% form_tag`
Ryan Bigg
@Ryan Yah, I left that out of my answer because there used to be a second answer here that mentioned it but I guess it was deleted.
Daniel Vandersluis