我没有在我的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>
...
答案 0 :(得分:0)
你缺少attr_accessible:
http://apidock.com/rails/ActiveRecord/Base/attr_accessible/class