0

I get this error when I load a page that contains a form (like edit.html.erb or new.html.erb). It freezes on the labels. I need to underline that I am not using ActiveRecord as an ORM in my app but Perpeuity gem which is an implementation of the Datamapper pattern. Leaving Activerecord behind has proved a bit problematic so maybe someone will be able to help.

The form partial:

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

      <ul>
        <% @address.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
<% end %>

<div class="row collapse">
  <div class="small-3 columns">
    <% require 'debugger'; debugger %>
    <%= f.label :line1, class: "right inline" %>
  </div>
  <div class="small-7 columns left">
    <%= f.text_field :line1 %>
  </div>
</div>

<div class="row collapse">
  <div class="small-3 columns">
    <%= f.label :line2, class: "right inline" %>
  </div>
  <div class="small-7 columns left">
    <%= f.text_field :line2, :placeholder => "(optional)"  %>
  </div>
</div>
<div class="row collapse">
  <div class="small-3 columns">
    <%= f.label :postcode, class: "right inline" %>
  </div>
  <div class="small-7 columns left">
    <%= f.select :postcode, Address::POSTCODES, :prompt => 'Select a postcode (optional)' %>
  </div>
</div>
<div class="row collapse">
  <div class="small-3 columns">
    <%= f.label :city, class: "right inline" %>
  </div>
  <div class="small-7 columns left">
    <%= f.text_field :city %>
  </div>
</div>
<div class="row collapse">
  <div class="small-3 columns">
    <%= f.label :county, class: "right inline" %>
  </div>
  <div class="small-7 columns left">
    <%= f.select :postcode, Address::COUNTIES, :prompt => 'Select a county (optional)' %>
  </div>
</div>
<div class="row collapse">
  <div class="small-9 small-offset-3 columns">
    <%= f.submit %>
  </div>
</div>

The model:

class Address
  extend ActiveModel::Naming
  extend ActiveModel::Translation
  include Perpetuity::RailsModel

  def errors
    @errors ||= ActiveModel::Errors.new(self)
  end

  def read_attribute_for_validation(attr)
    send(attr)
  end

 def save
   validate!
   errors.empty? ? super : false
 end

 def validate!
   #place custom validations here e.g.
   errors.add(:name, "cannot be blank.") if self.name = ""
   errors.add(:number,"must be less than 7.") if self.number >= 7
 end

 attr_accessor :name, :city, :county, :line1, :line2, :postcode
 attr_reader   :errors

end

The controller with relevant methods:

class AddressesController < ApplicationController

  def new
    @address = Address.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @address }
    end
  end

  def edit
    #require 'debugger'; debugger
    @address = Perpetuity[Address].find(params[:id])
  end

  def create
    @address = Address.new(params[:address])
    Perpetuity[Address].insert @address

    respond_to do |format|
      if @address.save
        format.html { redirect_to @address, notice: 'Address was successfully created.' }
        format.json { render json: @address, status: :created, location: @address }
      else
        format.html { render action: "new" }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @address = Perpetuity[Address].find(params[:id])

    respond_to do |format|
      if @address.update_attributes(params[:address])
        format.html { redirect_to @address, notice: 'Address was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end
end

I am using Rails 3.2 and ActiveModel gem installed has version 3.2.13. I had a look at the gem code and cannot find method i18n_key in it, while this documentation here [link]http://www.rubydoc.info/docs/rails/3.1.1/ActiveModel/Name:i18n_key indicates that it should be in there (similarly it is in 3.2.8 docs). Is it a bug or am I doing something wwrong? Thank your for your help.

evoo
  • 300
  • 4
  • 15
  • Actually extend ActiveModel::Naming, and extend ActiveModel::Translation should probably not be there but once I remove them I get undefined method `human' for Address:Class. Either way it is bad... – evoo Aug 07 '14 at 21:41
  • Looks like you found another thing that the `Perpetuity::RailsModel` class doesn't support. I didn't even know `ActiveModel` had i18n integration. The [API doc site](http://api.rubyonrails.org) don't mention it. I'll see what I can do with it. Do you remember which line was calling `i18n_key`? – jamie_gaskins Aug 16 '14 at 22:22

1 Answers1

0

I was getting this error and it turned out that I was using what appeared to be a reserved keyword in one of my classes. In my case it was model_name

Renaming it solved the issue for me.

Josh
  • 366
  • 2
  • 8
  • 20