Marty Wallace
Marty Wallace

Reputation: 35734

ActionController::UrlGenerationError missing required keys: [:id]

I am very new at ruby - trying out rails and I am stuck already trying to do a simple register form:

<%= form_for :user, url: user_path do |f| %>
    <p>
      <%= f.label :email %><br>
      <%= f.text_field :email %>
    </p>

    <p>
      <%= f.label :password %><br>
      <%= f.password_field :password %>
    </p>

    <p>
      <%= f.submit %>
    </p>
<% end %>

This is giving an error:

No route matches {:action=>"show", :controller=>"user"} missing required keys: [:id]

Can anyone explain what this actually means?

EDIT:

I am following this tutorial, only changing posts to user: http://guides.rubyonrails.org/getting_started.html

Upvotes: 10

Views: 9655

Answers (2)

Bala vamsi
Bala vamsi

Reputation: 222

I have faced a similar problem.

controller name should be plural (i.e users) and

the first line in new.html.rb should look like:

<%= form_for :user, url: users_path do |f| %>

Upvotes: 1

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11811

form_for should always get an object.. like a user from the controller

# controller
def new
  @user = User.new
end

# form
<%= form_for @user,...

Or you can use the form_tag method, which is not relying on an object..

Upvotes: 1

Related Questions