Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 224
Marty Wallace Avatar asked Jul 16 '13 22:07

Marty Wallace


2 Answers

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..

like image 121
BvuRVKyUVlViVIc7 Avatar answered Nov 15 '22 19:11

BvuRVKyUVlViVIc7


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| %>
like image 44
Bala vamsi Avatar answered Nov 15 '22 21:11

Bala vamsi