Rails:在视图中显示来自不同模型的表单 [英] Rails: Show form from different model in a view
本文介绍了Rails:在视图中显示来自不同模型的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 location
模型和一个 post
模型.我想允许用户从 location
模型的 index
页面创建新的 posts
.我怎么做?我尝试将 post
new.html.erb
渲染为 location
index.html.erb
中的一部分,但是这会导致错误,因为 post
new.html.erb
引用了 @post
变量.
I have a location
model and a post
model. I want to allow the user to create new posts
from the index
page of the location
model. How do I do that? I tried rendering the post
new.html.erb
as a partial in location
index.html.erb
, but this leads to errors as post
new.html.erb
references the @post
variable.
我应该如何做到这一点?
How should I accomplish this?
推荐答案
尝试这样的事情
LocationController
def index
@location = Location.find(:all)
@post = Post.new
end
现在你的位置视图可以访问实例变量@post
.使用该渲染部分
Now ur location view would have access to the instance variable @post
. Render a partial using that
render :partial => 'posts/post', :object => @post
这篇关于Rails:在视图中显示来自不同模型的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文