Topic: Beginner: form_for creating new object

Hi!

I just started out with rails and I am having some trouble with an error. I want to create a simple app where the user can create an object by filling in its attributes and attaching an image file. I googled and found railstutorial.org where form_for is used to create new user-objects. I tried following chapter 8 of this book but ended up with the following problem:

I created a user-model and two views by doing:
rails generate controller pages newuser displayuser
rails generate model user name:string email:string

this is my app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def newuser
    @user = User.new;
  end
end

this is my app/views/pages/newuser.html.erb
<%= form_for(@user) do |f| %>
    <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

When running the app with a blank app/views/pages/newuser.html.erb there are no problems, but after fillinf in the code above in this file I ended up with the following error message:
NoMethodError in pages#newuser
Showing /......./app/views/pages/newuser.html.erb where line #1 raised:
undefined method `users_path' for #<#<Class:0xb66fab4c>:0xb66f9d3c>

Why does this error occur? Do I need to add something to my config/routes.rb? Please help me create a simple form with no error messages!

Thanks in advance, Henk

Last edited by henk (2011-01-22 12:47:56)

Re: Beginner: form_for creating new object

You have to create routes for the user resource.

In 2.0:
config/routes.rb:

map.resources :users

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Beginner: form_for creating new object

Thanks! I am using 3.0.3 and I got it working by adding the line
resources :users

I am still having problems linking. what should I add to my config/routes.rb to be able to link using the line:
<%= link_to "new user", new_user %>

Thanks in advance /Henk

Last edited by henk (2011-01-22 19:21:15)

Re: Beginner: form_for creating new object

<%= link_to "new user", new_user_path %>

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Beginner: form_for creating new object

ok! it works! is the following correct?

a path is written in the syntax: ONE_TWO_THREE where ONE == the name of the page without file ending,  TWO == the name of the folder in app/views/ the page is in minus the last character ("s") and THREE == "path"

when I try to link to app/views/pages/home.html.erb I guessed that that path name should be
home_page_path

but when visiting the page I ended upp with the error
undefined local variable or method `home_page_path' for #<#<Class:0xb659e5f0>:0xb659d7b8>

What should home_page_path be, and how is this normally done?

Thanks

Re: Beginner: form_for creating new object

issue the command:

rake routes

look at the output,  it will show you the available url helpers (minus the _path)

with this route:

resources users

you'll get these path helpers

users_path
new_user_path
edit_user_path
user_path

normally,  unless you modified your out of the box routes,  to get to the home page of the app,

link_to 'Home', '/'
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Beginner: form_for creating new object

thanks!