关联数据库如何获取其外键?
我对默认控制器做了很多修改,现在我无法在创建时填充我的 user_id 。
我很好奇它是如何填充的。如果您有兴趣,请查看我的路线和模型关联,然后最后我将向您展示生成的参数。最终结果是没有添加 user_id
。
routes
resources :users do
resources :posts do
collection do
get :view
end
end
end
models
#Post.rb
belongs_to :user, :touch => true
#User.rb
has_many :posts
我导航到这个网址..
http://localhost:3001/users/1/posts/new
并放置我的帖子,params
返回这个:
{"commit"=>"save", "post"=>{"name"=>"hell hath no furry", "category"=>"vegan", "url"=>"www.reddit.com", "text"=>"", "is_link"=>"1"}, "authenticity_token"=>"aYnSLgJ9E6MaM6iSkRrCyyiMZj06oLdybTMkNqss8FA=", "utf8"=>"✓", "action"=>"create", "controller"=>"posts"}
这之前是有效的,不知道为什么它不再关联它们。
有什么想法吗?
I did a bunch of gutting to my default controllers and now I can't get my user_id to populate on create.
I was curious how that gets populated. If you're interested, take a look at my routes, and model assocations, and then at then end I'll show you the resulting params..The end result is no user_id
being added.
routes
resources :users do
resources :posts do
collection do
get :view
end
end
end
models
#Post.rb
belongs_to :user, :touch => true
#User.rb
has_many :posts
I navigate to this url..
http://localhost:3001/users/1/posts/new
and place my post, and the params
return this :
{"commit"=>"save", "post"=>{"name"=>"hell hath no furry", "category"=>"vegan", "url"=>"www.reddit.com", "text"=>"", "is_link"=>"1"}, "authenticity_token"=>"aYnSLgJ9E6MaM6iSkRrCyyiMZj06oLdybTMkNqss8FA=", "utf8"=>"✓", "action"=>"create", "controller"=>"posts"}
This was working before, not sure why its not associating them anymore.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 form_for 中,您是否传递了@user?
也许这会有所帮助:
http:// www.gatezero.org/blog/2008/4/30/rails-nested-resources-and-form_for.html
In your form_for, are you passing in the @user?
Maybe this will help:
http://www.gatezero.org/blog/2008/4/30/rails-nested-resources-and-form_for.html
嗯,我不喜欢回答自己的问题,但我找到了这个解决方案,并且想知道这是否是实现此目标的可接受的方式。
在我的
def create
控制器中,我这样做:而不是这样:
Hmm I don't like answering my own questions, but I found this solution, and was wondering if this is an acceptable way of accomplishing this.
In my controller for
def create
, I do this :instead of this :