我做了一堆内脏到我的默认控制器,现在我无法让我的user_id填充创建。关联的数据库如何获取其外键?
我很好奇这是如何得到填充。如果你有兴趣,看看我的路线和模型关联,然后在最后我会告诉你结果params ..最终的结果是没有user_id
被添加。
路线
resources :users do
resources :posts do
collection do
get :view
end
end
end
模式
#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"}
这是工作之前,不知道为什么它不再联系他们了。
任何想法?
是的,这看起来像一个伟大的方式来做到这一点。如果你正在基于current_user(我不确定)做什么,你可能想要使资源映射单一,因为你根本没有在路由中使用user_id。 http://guides.rubyonrails.org/routing.html#singular-resources – njorden 2010-10-29 22:38:13