form_for undefined method `model_name' for ::ActiveRecord_Relation:Class

DMH picture DMH · Apr 28, 2014 · Viewed 13k times · Source

I have been having issues with form_for rendering. I want to have a _form.html.erb partial that deals with both the creation and editing of my Problem record. When I go down the route of using

<%= form_for(@problem) do |f| %>

I get the following error:

undefined method `model_name' for Problem::ActiveRecord_Relation:Class

In my routes.rb I have the following:

PW::Application.routes.draw do
root to: 'problems#index', via: :get
resources :problems do
member do
  post 'up_vote'
  post 'down_vote'
 end
end

I also have this my in Problem Controller

class ProblemsController < ApplicationController
 include Concerns::Votes

 def new
  @problem = Problem.new
 end

 def index
 @problem = Problem.all
 end

 def show
 @problem = find_problem
 end

 def create
 @problem = current_user.problems.new(problem_params)
 @problem.save
 redirect_to @problem
 end


private

 def find_problem
 @problem = Problem.find(params[:id])
end

 def problem_params
params.require(:problem).permit(:name, :description, :url)
end
end

I can get it to work if I specify the following :

<%= form_for @problem.new, url: {action: "create"} do |f| %>

However I feel that this is repeating myself if I then have to make a separate partial just for the edit. I really cant work out why this doesn't want to work. Could it be I am rendering the form on index.html.erb?

Any help or direction would be very much appreciated.

Answer

Richard Peck picture Richard Peck · Apr 28, 2014

LOL why didn't I see this earlier?

Could it be I am rendering the form on index.html.erb?

Yep, it's totally the problem. Reason is here:

Problem::ActiveRecord_Relation:Class

You're getting back a relation object (defines a collection, rather than single record). This is caused by this:

#controller
def index
   @problem = Problem.all
end

The error is beacuse form_for expects a single record. You need to use this in your controller & it will work:

#controller
def index
   @problem = Problem.new
   @problems = Problem.all
end

#view
<%= @problems.each do |problem| %>
    <%= problem.name %>
<% end %>

<%= form_for @problem do |f| %>