I'm trying to make two forms for two different models in the same view.
I have a model named category and a model named post. Im trying to make a form for categories in the same view i have a form for posts. The form for posts works fine, but when i'm trying to add the form for categories i get this error: undefined method `model_name' for Category::ActiveRecord_Relation:Class
category.rb - model
has_many :posts
post.rb - model
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
posts view - 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'
I have tried to search after if, but i can only find solutions for two models, one form.
Thanks in advance. :-)
Since you say its in the index action:
def index
@post = Post.new
@category = Category.new
end
In your view:
<%= form_for(@post) do |f| %>
<%= f.text_field :title %>
<%= f.text_area :content %>
<%= f.submit %>
<% end %>
<%= form_for(@category) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>