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

Ank*_*oni 6 ruby-on-rails partial ruby-on-rails-3

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

我该怎么做到这一点?

Rah*_*hul 16

尝试这样的事情

LocationController

def index
  @location = Location.find(:all)
  @post = Post.new
end
Run Code Online (Sandbox Code Playgroud)

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

render :partial => 'posts/post', :object => @post
Run Code Online (Sandbox Code Playgroud)