這樣做是如下
@user = User.find(session[:user_id])
article = @user.articles.build(params[:article])
article.save
假設params[:article] = {:title => "Test Title", :description => "Test Description"}
然後
article = @user.articles.build(params[:article])
正確方法是一樣的
article = Article.new(:title=>params[:article][:title],
:description =>params[:article][:description],
:user_id => @user.id)
這意味着These objects will be instantiated from the passed attributes, and the link through their foreign key (i.e. 'user_id' in your case) will be created, but the associated objects will not yet be saved.
參考this更多細節
注: - 我假設用戶有很多文章
@tereško這個問題是如何尋求幫助和最佳實踐的請求在真實案例中處理MVC架構設計,以達到良好的軟件設計目的,但同時(也是最重要的)爲了避免潛在的安全風險,爲什麼要關閉它? 跑出高清空間? :D –