STI and form_for problem
Asked Answered
L

4

16

I am using Single Table Inheritance for managing different types of projects.

Models:

class Project < ActiveRecord::Base
end

class SiteDesign < Project
end

class TechDesign < Project
end

Edit action from projects_controller:

def edit
   @project = Project.find(params[:id])
end

View edit.html.erb:

<% form_for(@project, :url => {:controller => "projects",:action => "update"}) do |f| %>
    ...
    <%= submit_tag 'Update' %>
<% end %>

Update action of projects_controller:

def update
    @project = Project.find(params[:id])
    respond_to do |format|
      if @project.update_attributes(params[:project])
        @project.type = params[:project][:type]
        @project.save
        flash[:notice] = 'Project was successfully updated.'
        format.html { redirect_to(@project) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

Then i do some edits of TechDesign entry on edit view and get error:

NoMethodError in ProjectsController#update

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

In parametrs it is obvious that instead of project parameter name i have tech_design Parameters:

{"commit"=>"Update",
 "_method"=>"put",
 "authenticity_token"=>"pd9Mf7VBw+dv9MGWphe6BYwGDRJHEJ1x0RrG9hzirs8=",
 "id"=>"15",
 "tech_design"=>{"name"=>"ech",
 "concept"=>"efds",
 "type"=>"TechDesign",
 "client_id"=>"41",
 "description"=>"tech"}}

How to fix it?

Lesleylesli answered 16/10, 2009 at 15:4 Comment(0)
C
26

Here's the source of your problem. This is setting @project as an instance of a TechDesign object.

def edit
   @project = Project.find(params[:id])
end

You can ensure things work the way you want by specifying :project for a name in the form_for call.

<% form_for(:project, @project, :url => {:controller => "projects",:action => "update"}) do |f| %>
    ...
    <%= submit_tag 'Update' %>
<% end %>
Cytologist answered 16/10, 2009 at 16:20 Comment(4)
Thanks for your reply. After those chages i've got this error : No action responded to 1. Actions: create, destroy, edit, index, new, show, and updateLesleylesli
After adding this map.connect "projects/:id/edit", :controller => "projects", :action => "edit" to routes.rb error disapeared. Question is why it does not worked without it if map.resources :projects already presents in routes?Lesleylesli
In Rails 3.1, write the form_for statement as <%= form_for @project, :as => :project do |f| %>Revelry
Old thread, but since I found it when Googling, this is what I did: henrik.nyh.se/2012/08/rails-sti-and-form-for Note that :as => :project will only influence the params, not the URL.Immethodical
B
7

For Rails 3

<% form_for(@project, :as => :project, :url => {:controller => "projects",:action => "update"}) do |f| %>
...
   <%= submit_tag 'Update' %>
<% end %>
Boatright answered 21/9, 2012 at 15:3 Comment(1)
this works with simple_form_for as well. e.g. simple_form_for(@web_hook, as: :web_hook, url: url)Neonatal
H
2

A random note: If you are using single table inheritance (STI) and forget to remove the initialize method from your subclass definitions you will get a similar "nil object when you didn't expect it" exception.

For example:

class Parent < ActiveRecord::Base
end

class Child < Parent
  def initialize
    # you MUST call *super* here or get rid of the initialize block
  end
end

In my case, I used my IDE to create the child classes and the IDE created the initialize method. Took me forever to track down...

Hesler answered 18/6, 2010 at 14:26 Comment(0)
A
2

For Rails 4, I have confirmed the only thing that has seemed to work for me is to explicitly specify the URL and the AS parameters:

<% form_for(@project, as: :project, url: {controller: :projects, action: :update}) do |f| %>
    ...
<% end %>

Seems ugly to me!

Arsenite answered 20/5, 2015 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.