David Oneill
David Oneill

Reputation: 13075

Ruby on rails: start_form_tag method

I'm trying to learn ruby on rails. I've been going through a tutorial, but I've gotten stuck.

It has me using start_form_tag and end_form_tag around an input form. However, when I access the page, I get undefined method 'start_form_tag' for #<ActionView::Base:0x2556020>

In the tutorial, they explain that these two lines are translated into <form action="/book/create" method="post"> and </form>. As such, I tried putting those instead. The form out come up, but when I submitted the form, I get this error: ActionController::InvalidAuthenticityToken in BookController#create

So,

  1. What do I have to do to get start_form_tag to translate correctly?
  2. Is this causing the InvalidAuthenticityToken error?

Upvotes: 3

Views: 3619

Answers (3)

YuZhen
YuZhen

Reputation: 31

Try this:

<h1>Add new book</h1>
<%= form_tag :action => 'create' %>
<p><label for="book_title">Title</label>:
<%= text_field 'book','title' %></p>
<p><label for="book_price">Price</label>:
<%= text_field 'book','price'%></p>
<p><label for="book_subject">Subject</label>:
<%= collection_select(:book,:subject_id,@subjects,:id,:name)%></p>
<p><label for="book_description">Description</label><br/>
<%= text_area 'book','description'%></p>
<%= submit_tag "Create"%>
<%= link_to 'Back',{:action=>'list'}%>

Upvotes: 3

klew
klew

Reputation: 14967

I had the same problem when I started learning Rails. You have tutorial for old Rails version. start_form_tag is no longer used. I think the best place to learn Rails is Rails Guides

So, your question now. You can add form like this:

<% form_for @book do |f| %>
  <%= f.label :title %>
  <%= f.text_filed :title %>
  ...
  <%= f.submit 'Create' %>
<% end %>

Upvotes: 9

Veeti
Veeti

Reputation: 5300

The tutorial you're reading is outdated. Forms are now built using form_for blocks.

You can find a more up-to-date (and official) guide here. You can probably use it to complete the tutorial you're doing now.

Upvotes: 4

Related Questions