3

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>
...
codinghands
  • 1,741
  • 2
  • 18
  • 31

1 Answers1

0

You're missing attr_accessible:

http://apidock.com/rails/ActiveRecord/Base/attr_accessible/class

bluehallu
  • 10,205
  • 9
  • 44
  • 61
  • Like `attr_accessible :title, :author, :handle, :metafield`? I had attempted this earlier and got a variety of seemingly unrelated errors - `undefined method `each' for nil:NilClass` being one, `undefined method `attr_accessible' for Page:Class` being another. I thought attr_accessible was just for ActiveRecord (not Resource). Still, I'm a total RoR newb so... – codinghands Jun 25 '13 at 11:23
  • If you need to mass assign (that is, setting multiple variables at once, like when submiting a form) you need to specify that those attributes are mass assignable, otherwise it will say that there's no such attribute. If what you need is to access them individually (that is, with getters and setters), use attr_accessor. – bluehallu Jun 25 '13 at 13:08
  • That's exactly what this is doing I suppose, as it's effectively an HTTP POST. How do I specify attributes as 'mass assignable'? – codinghands Jun 25 '13 at 17:25
  • I mentioned I had attempted using 'attr_accessible' which resulted in the errors in my first comment. I'm using ActiveRESOURCE ( not ActiveRECORD) – codinghands Jun 26 '13 at 02:53