3

RoR ActiveResource モデルでスキーマを定義せずに「新しい」RESTful エンティティを追加することはできません。これはオプションであると読んだことがあります。スキーマを省略すると (わかりやすくするために以下のコードに含めています)、次のエラーが表示されます。

NoMethodError in Pages#new

undefined method `author' for #<Page:0x4823dd8>

スキームを含めると、すべてが期待どおりに機能します。スキーマが変更されるたびに追跡する必要はなく、スキーマを必要としない例を見てきました。他の RESTful 動詞 (読み取り、更新、および削除) はすべて、スキームなしで機能します。どこが間違っていますか?それが重要な場合、私rails generate scaffoldは足場を作成していました。


コントローラ:

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

モデル:

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

ビュー: (簡潔にするためにカット)

...
<%= 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>
...
4

1 に答える 1

0

attr_accessible がありません:

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

于 2013-06-25T11:11:50.823 に答える