Rails:从视图中的不同模型中显示表格
我有一个location
模型和一个post
模型。我希望允许用户从location
模型的index
页面创建新的posts
。我怎么做?我试图将post
new.html.erb
作为location
index.html.erb
中的一部分进行渲染,但这会导致错误,因为post
new.html.erb
引用了@post
变量。Rails:从视图中的不同模型中显示表格
我该如何做到这一点?
尝试是这样的
LocationController
def index
@location = Location.find(:all)
@post = Post.new
end
现在乌尔位置视图将有机会获得实例变量@post
。使用
render :partial => 'posts/post', :object => @post
只需在LocationsController的操作中创建一个@post
变量。
这给了我这个错误渲染的部分:'预计/home/eyekandy/rails_apps/eyekandy/app/controllers/locations_controller.rb定义LocationsController' –
然后你有**语法错误**在你的代码中。你能展示你的行为代码吗?我假设你只编辑了动作,但错误可能在其他地方 - 也许你不小心删除了一些字符? – Arsen7
可以启动形式是这样的:
<%= form_for Post.new do ... %>
+1,..或者类似于Allesklar提出的方式:使用Post.new而不是变量,但这取决于视图:变量可能在那里被'硬编码'。 – Arsen7