【发布时间】:2014-09-13 15:43:22
【问题描述】:
我正在尝试在同一个视图中为两个不同的模型制作两个表单。
我有一个名为 category 的模型和一个名为 post 的模型。 我试图在同一视图中为类别制作表格,我有一个帖子表格。 帖子的表单工作正常,但是当我尝试为类别添加表单时,我收到此错误: Category::ActiveRecord_Relation:Class 的未定义方法 `model_name'
category.rb - 模型
has_many :posts
post.rb - 模型
has_many :categories
posts_controller
def index
@posts = new.Post
@categories = new.Category
end
def create
@posts = Post.create(post_params)
@posts.save
redirect_to :back
end
def create_cate
@categories = Categroy.create(categories_params)
@categroies.save
redirect_to :back
end
帖子视图 - index.html.erb
<%= form_for(@posts) do |f| %>
<%= f.text_field :title %>
<%= f.text_area :content %>
<%= f.submit %>
<% end %>
<%= form_for(@categories) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
routes.rb
resources :posts
resources :categories
root 'posts#index'
我试过搜索if,但我只能找到两种模型,一种形式的解决方案。
提前致谢。 :-)
【问题讨论】:
标签: ruby-on-rails ruby forms ruby-on-rails-4 models