我使用Ruby on Rails創建應用程序,並在管理面板中有blogs
和posts
控制器。爲管理區的路線是這樣的:Rails使用博客和帖子構建方法嵌套資源
constraints :subdomain => "admin" do
scope :module => "admin" do
root to: "pages#index"
resources :blogs do
resources :posts, :controller => "posts"
end
end
end
我有什麼http://admin.mydomain.com/blogs
顯示與/blogs/2/
出現在以博客帖子的博客。
我想要的是當在/blogs/2/posts/new
上創建一個新帖子時,blog_id
被附加到帖子上。
在admin/posts_controller.rb
我有這個作爲創建行動
def create
@post = Post.new(params[:post])
if @post.save
redirect_to posts_path, notice: 'Post was successfully created.'
else
render action: "new"
end
end
在它剛剛創建帖子的時刻。我想將該帖子鏈接到網址中的當前博客ID - /blog/2
。
我怎麼會去這樣做呢?
輝煌。這工作馬上,謝謝亞當:) – 2012-03-16 19:12:27