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
变量。