Question

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.

Was it helpful?

Solution

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| %>

OTHER TIPS

You might be using @problems, as you said the form is in index page. If you are doing so then the index action code should be like

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

You should have a create method defined in your controller like this

def create
 @problem = Problem.new

 #other logic goes here
end

And your form_for for create action should be like this

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

I was getting a similar error:

undefined method `model_name' for form_for

It was for a simple reason:

I was using form_for, where I should have been using form_with. As soon as I changed that it worked as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top