Question

Browsing the excellent Rails tutorial by Michael Hartl (Chapter 8), I'm trying to use the form_tag helper provided by Rails in my (Haml) view. The form passed by form_tag to the block has value nil, which I guess is not the intended use, since it prevents to generate fields in the form. I don't understand what I'm doing wrong.

Here the haml code:

= form_tag(sessions_path) do |f|
  = # f.label :email # undefined method `label' for nil:NilClass
  = f.class.name     # NilClass
= form_for(:session, url: sessions_path) do |f|
  = f.label :email   # Email
  = f.class.name     # ActionView::Helpers::FormBuilder 

Here the HTML that the first part of it (form_tag) generates:

<form accept-charset="UTF-8" action="/sessions" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="c72gY3kuFKTFxNerFWT5fH1n63cp9U8Wj0+UBTllqPQ=" /></div>

nil
</form>

On the contrary, the form_for have the expected behavior.

I could not find answers searching on the internet: In the RailsCast 270 about Authentication (around 3:30), the video shows code that seems similar to mine. I did not find related answers when searching with such keywords as form_tag, NilClass or haml on Stackoverflow and Google.

Would anyone have a clue about what I did wrong? Thanks!

Was it helpful?

Solution

You can have a look at this other SO question to understand the difference between form_for and form_tag.

In the Railscasts episode you mentioned, the code looks like this:

= form_tag(sessions_path) do
  = label_tag :email
  = text_field_tag :email, params[:email]

Which is not similar to your code!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top