Reputation: 19684
I am just beginning to work with Rails. I have a model, "User" and it has many "List". On the user show view I have created a form to create a list for the current user. When the form is submitted I get the following error:
Couldn't find User without an ID
I assume that there is something wrong with the way I have setup the nested List form.
User -> show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Username:</b>
<%= @user.username %>
</p>
<p>
<b>Password:</b>
<%= @user.password %>
</p>
<p>
<b>Email:</b>
<%= @user.email %>
</p>
<h2>Add a List</h2>
<%= form_for([@user, @user.lists.build]) do |f| %>
<div class ="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%=f.label :description%><br />
<%=f.text_field :description%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit User', edit_user_path(@user) %> |
<%= link_to 'Back to Users', users_path %>
This is the list 'create' controller action.
class ListsController < ApplicationController
def create
@user = User.find(params[:id])
@list = @user.lists.create(params[:list])
redirect_to user_path(@user)
end
end
Update: show method on UserController
# GET /users/1
# GET /users/1.json
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
Update: My routes
resources :users do
resources :lists
end
get "home/index"
So is params[:id] nil somehow? What am I missing?
Thanks!
Upvotes: 0
Views: 5137
Reputation: 9221
At least in Rails 4 (not sure about previous versions), if you pass a param in the form for something in the new view, if you want to call that param in the create action, you need to slightly modify the call to resource_name_params[:user_id]
rather than simply params[:user_id]
.
Specifically in your case, your create method in the controller may need to look like this instead:
class ListsController < ApplicationController
def create
@user = User.find(list_params[:user_id])
@list = @user.lists.build(list_params)
...
Upvotes: 0
Reputation: 12818
Make sure you are using params[:user_id]
in create action, not params[:id]
class ListsController < ApplicationController
def create
@user = User.find(params[:user_id])
@list = @user.lists.create(params[:list])
redirect_to user_path(@user)
end
end
Upvotes: 1
Reputation: 86
Can you try this? Since this isn't RESTUL routing (see http://guides.rubyonrails.org/form_helpers.html) you might have to specify the URL (controller and action) you want the form to hit in the form_for tag. If this still isn't passing the user id, you can also use a hidden_field to pass it explicitly in the form. Let me know if this doesn't do the trick...
In the view (show.html.erb)
<h2>Add a list</h2>
<%= form_for [@user, @list] do |f| %>
<div class ="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
<%=f.label :description%><br />
<%=f.text_field :description%>
<%= f.submit %>
<% end %>
In the Users controller:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@list = @user.list.new
end
end
And in the Lists controller:
class ListsController < ApplicationController
def create
@user = User.find(params[:user_id])
@list = @user.lists.build(params[:list])
@list.save
redirect_to @user # or wherever
end
end
Upvotes: 1