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 回答