0

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

Community
  • 1
  • 1
SnareChops
  • 13,175
  • 9
  • 69
  • 91

1 Answers1

1

So.... I feel like an idiot. I was reading the error message, but not actually reading the error message.

The issue was in my show.json.jbuilder file. I had an account_list_url(@list) call which was clearly incorrect. I thought I had checked there but apparently not.

Take home message: If the error message shows a stack trace with jbuilder in it. Look in the jbuilder file...

SnareChops
  • 13,175
  • 9
  • 69
  • 91