我有一个 location
模型和一个 post
模型。我想允许用户从 location
模型的 index
页面创建新的 posts
。我怎么做?我尝试将 post
new.html.erb
渲染为 location
index.html.erb
中的部分内容,但是这会导致错误,因为 post
new.html.erb
引用了 @post
变量。
我应该如何实现这个目标?
最佳答案
尝试这样的事情
LocationController
def index
@location = Location.find(:all)
@post = Post.new
end
现在您的位置 View 可以访问实例变量@post
。使用它渲染部分
render :partial => 'posts/post', :object => @post
关于ruby-on-rails - rails : Show form from different model in a view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188999/