Ruby on rails Rails:在视图中显示来自不同模型的表单
我有一个Ruby on rails Rails:在视图中显示来自不同模型的表单,ruby-on-rails,ruby-on-rails-3,partial,Ruby On Rails,Ruby On Rails 3,Partial,我有一个location模型和一个post模型。我想允许用户从位置模型的索引页面创建新的帖子。我该怎么做?我尝试将postnew.html.erb呈现为位置index.html.erb中的一部分,但这会导致错误,因为postnew.html.erb引用了@post变量 我应该如何完成此操作?只需在LocationsController中的操作中创建一个@post变量。您可以这样启动表单: <%= form_for Post.new do ... %> 试试这样的方法 Locatio
location
模型和一个post
模型。我想允许用户从位置
模型的索引
页面创建新的帖子
。我该怎么做?我尝试将post
new.html.erb
呈现为位置index.html.erb
中的一部分,但这会导致错误,因为post
new.html.erb
引用了@post
变量
我应该如何完成此操作?只需在LocationsController中的操作中创建一个@post
变量。您可以这样启动表单:
<%= form_for Post.new do ... %>
试试这样的方法
LocationController
def index
@location = Location.find(:all)
@post = Post.new
end
现在,您的位置视图可以访问实例变量@post
。使用该
render :partial => 'posts/post', :object => @post
这给了我一个错误:Expected/home/eyekandy/rails\u apps/eyekandy/app/controllers/locations\u controller.rb若要定义LocationsController
,则代码中存在语法错误。你能告诉我你的行为准则吗?我假设您只编辑了操作,但错误可能在其他地方-可能您意外删除了某些字符?+1,…或者,以类似Allesklar建议的方式:使用Post.new
而不是变量,但这取决于视图:变量可能在那里“硬编码”。