I have a Rails 4.2.beta1 application and I am trying to return the :show
method after successful creation of a new object. The resource is nested in the following method:
resources :accounts do
resources :lists do
end
The error I am getting is:
ActionController::UrlGenerationError (No route matches {
:account_id=>#<List id: 21, creator_id: 1, list_type_id: 1,
name: "Test 16", description: nil,
created_at: "2014-10-07 03:59:56",
updated_at: "2014-10-07 03:59:56">,
:action=>"show", :controller=>"lists", :format=>nil, :id=>nil}
missing required keys: [:id]):
app/views/lists/show.json.jbuilder:2:in `_app_views_lists_show_json_jbuilder__4188070110312340824_70141816016640'
app/controllers/lists_controller.rb:31:in `create'
What I see here is that rails is incorrectly thinking that the new List
object is the account_id
...
Here is my create
action:
# POST /lists
# POST /lists.json
def create
@list = List.new(creator: @account, list_type: ListType.find(list_params[:list_type_id]), name: list_params[:name])
if @list.save
@account.subscriptions << @list
render action: :show, status: :created, location: [@account, @list] and return
end
render json: @list.errors, status: :unprocessable_entity
end
I found this Q/A here and followed all of the answers there with no change in result. To list them out I have tried ALL of the following...
#1
render action: :show, status: :created, location: [@account, @list] and return
#2
render action: 'show', status: :created, location: [@account, @list] and return
#3
render action: :show, status: :created, location: [@list.creator, @list] and return
#4
render action: :show, status: :created, location: @list and return
#5
render action: :show, status: :created, location: [@list, @account] and return
#6
render action: :show, status: :created, location: [:account, @list] and return
#7
render action: :show, status: :created, location: accounts_list_url(@account, @list) and return
#8
render action: :show, status: :created and return
#9
redirect_to action: :show, status: :created, location: [@account, @list] and return
#10
redirect_to accounts_lists_url(@account, @list) and return
All of those produce the same error (or an obvious error like having the ids in the wrong order like in #5)
Another thing to note is that #7 && #10 produce the error:
NoMethodError (undefined method `accounts_lists_url' for #<ListsController:0x007f9652235c40>)
Which also seems really strange to me