I'm unable to add 'new' RESTful entities without defining a schema in my RoR ActiveResource model, even though I've read this is optional. When omitting the schema (I've included it in the code below for clarity), I receive the following error:
NoMethodError in Pages#new
undefined method `author' for #<Page:0x4823dd8>
When I include the scheme, everything works as expected. I'd really rather not have to keep track of the schema every time it changes, and I've seen examples that don't require it. The other RESTful verbs (read, update & delete) all work without the scheme. Where am I going wrong? I used rails generate scaffold
to create the scaffolding, if that's important.
Controller:
class PagesController < ApplicationController
around_filter :shopify_session, :except => 'welcome'
...snip...
def new
@page = Page.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @page }
end
end
def edit
@page = Page.find(params[:id])
end
def create
@page = Page.new(params[:page])
respond_to do |format|
if @page.save
format.html { redirect_to @page, notice: 'Page was successfully created.' }
format.json { render json: @page, status: :created, location: @page }
else
format.html { render action: "new" }
format.json { render json: @page.errors, status: :unprocessable_entity }
end
end
end
...snip...
end
Model:
class Page < ShopifyAPI::Page #Note: ShopifyAPI::Page extends ActiveResource::Base
schema do
string :author
string :body_html
string :title
string :metafield
string :handle
end
end
View: (cut for brevity)
...
<%= form_for(@page) do |f| %>
<% if @page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% @page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :author %><br />
<%= f.text_field :author %>
</div>
...