Reputation: 30256
My controller action is supposed to respond_with
a newly created User::SmtpConfig
record, whose model is embedded inside a User record, but it misidentifies user_id
for the new record and raises a UrlGenerationError
.
Error
ActionController::UrlGenerationError at /users/531604866465623cca000000/smtp_configs.json
No route matches {:action=>"show", :controller=>"users/smtp_configs", :user_id=>#<User::SmtpConfig _id: 537a84af6465620a1f090000, uname: "f", pwd: "a", host: "fdsaf", addr: "foo.bar.com", port: nil, tls: nil, auth: nil>, :format=>nil, :id=>nil} missing required keys: [:id]
Controller action
respond_to :json
# This action is reached with the following URL path:
# /users/:user_id/smtp_configs/:id.json
def create
user = User.find(params[:user_id])
@smtp = user.smtp_configs.create params
@smtp.id # => BSON::ObjectId('537a85286465620a1f0a0000')
@smtp.user_id # => BSON::ObjectId('531604866465623cca000000')
respond_with @smtp
end
Why is the controller trying to match any route at all? And why is user_id
evaluating to the newly-created object itself?
Upvotes: 1
Views: 170
Reputation: 30256
In the end, my solution was to supply a url as a location
option in my respond_with
call:
def create
@user = User.find(params[:user_id])
@smtp = @user.smtp_configs.create(resource_params)
respond_with @smtp, location:polymorphic_url(@smtp, {user_id:@user, id:@smtp.id, format: :json})
end
Upvotes: 1