ruby-on-rails - rails : Show form from different model in a view

标签 ruby-on-rails ruby-on-rails-3 partial

我有一个 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/

相关文章:

sql - 如何改变Rails的belongs_to行为

ruby-on-rails - 如何将 Rails 中的自定义环境设为默认环境?

c# - 如何将图像的一部分插入图片框?

file - C程序如何写入文本文件

Python 装饰器返回一个替换了一个或多个参数的函数

ruby-on-rails - 如何解决 Nokogiri 解析缓慢的问题

javascript - amcharts 与 Ruby on Rails 的集成 : Using data from the database to populate the graph

ruby-on-rails - 为什么每当我登录 Rails 应用程序时,都会收到 "PG::NotNullViolation null value in column "id“违反非空约束”?

ruby-on-rails - 每个模块的不同部分 View

ruby-on-rails - 有没有更 ruby 的方式来做这个逻辑