[英]Mongoid: respond_with newly-created embedded object raises UrlGenerationError
我的控制器动作应该respond_with
新创建的User::SmtpConfig
记录,它的模型嵌入用户记录里面,但它misidentifies user_id
新记录,并提出了一个UrlGenerationError
。
错误
位于/users/531604866465623cca000000/smtp_configs.json的ActionController :: UrlGenerationError
没有路由匹配{:action =>“ show”,:controller =>“ users / smtp_configs”,:user_id =>#<User :: SmtpConfig _id:537a84af646562020a1f090000,uname:“ f”,pwd:“ a”,主机: “ fdsaf”,addr:“ foo.bar.com”,端口:nil,tls:nil,auth:nil> 、: format => nil,:id => nil}缺少必需的键:[:id]
控制器动作
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
为什么控制器要尝试完全匹配任何路由? 以及为什么user_id
对新创建的对象本身求值?
最后,我的解决方案是在我的respond_with
调用中提供一个URL作为location
选项:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.