Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 error: can't write unknown attribute `html'

Running into an error trying to set up a basic Rails 4 app for learning purposes, so bear with me! I am trying to create an app to create and display custom web forms. I have a Form model, which has many Fields. I'm at the point where I'm trying to get the view working that will allow me to create a new Field record attached to a specific Form:

class Form < ActiveRecord::Base    
    has_many :fields    
end

class Field < ActiveRecord::Base
    belongs_to :form
end

On my Field index view, which I believe I have set up to correctly to only show the Fields of a specific form (via a url like /forms/1/fields), I have a link as such:

<%= link_to 'New Field', new_form_field_path(@form) %>

The fields/new.html.erb file has this:

<h1>New field</h1>
<%= render :partial => 'form', :form => @form, :field => @field %>

And the fields/_form.html.erb starts like this:

<%= form_for(@form, @field) do |f| %>

The fields_controller.rb has this method defined:

def new
  @form = Form.find(params[:form_id]) #unsure if this is necessary/correct, but its presence doesn't effect the error i'm getting
  @field = Field.new
end

A Form with id 1 has already been created. It looks like /forms/1/fields comes up ok. But when I click the "New Field" link, which takes me to /forms/1/fields/new, I get this error:

Showing /home/moskie/Projects/FormBuilder/app/views/fields/_form.html.erb where line #1 raised:

can't write unknown attribute `html'
Extracted source (around line #1):

  <%= form_for(@form, @field) do |f| %>
    <% if @field.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@field.errors.count, "error") %> prohibited this field from being saved:</h2>

Trace of template inclusion: app/views/fields/new.html.erb

Rails.root: /home/moskie/Projects/FormBuilder

Application Trace | Framework Trace | Full Trace
app/views/fields/_form.html.erb:1:in `_app_views_fields__form_html_erb___1866877160086017450_70350628427620'
app/views/fields/new.html.erb:3:in `_app_views_fields_new_html_erb___1515443138224133845_70350627074400'
Request

Parameters:

{"form_id"=>"1"}

I'm pretty confused by what this error is telling me, so I'm having trouble figuring out what I've done wrong here. Can anyone help me out? Thanks.

like image 254
Moskie Avatar asked Sep 23 '13 01:09

Moskie


1 Answers

Got it. The call to form_for in the _form.html.erb Field partial view needed square brackets, instead of the parenthesis. The method wants an array of the two objects as its first parameter in this case, not to have the two objects passed in separately:

<%= form_for [@form, @field] do |f| %>
like image 182
Moskie Avatar answered Oct 12 '22 02:10

Moskie