Rails:在视图中显示来自不同模型的表单

我有一个location模型和一个post模型。 我想允许用户从location模型的index页面创建新posts 。 我怎么做? 我尝试将new.html.erb作为部分location index.html.erb渲染,但这会导致错误,因为post new.html.erb引用@post变量。

我该怎么做到这一点?

尝试这样的事情

 LocationController def index @location = Location.find(:all) @post = Post.new end 

现在,您的位置视图可以访问实例变量@post 。 使用它渲染部分

 render :partial => 'posts/post', :object => @post 

您可以像这样启动表单:

 <%= form_for Post.new do ... %> 

只需在LocationsController中的动作中创建一个@post变量。