1

作为我为 Shopify 网站进行的自定义的一部分,我正在尝试编写一个只允许编辑特定元字段的工具。

在索引页面上,我将列出与特定元字段相关联的各种集合、产品、页面和博客文章,我希望用户能够通过表单进行编辑。

......对于我的生活,我只是看不到我错过了什么!非常感谢解决此问题的任何帮助:)

所以……我们走吧!当用户单击“编辑”(应该将他们带到一个表单以编辑该集合、产品、页面或博客文章的特定元字段)时,我在浏览器中收到此错误:

NoMethodError in Metafields#edit

Showing /Users/james/code/test-app/app/views/metafields/_form.html.erb where line #1 raised:

undefined method `shopify_api_metafield_path' for #<#<Class:0x007facf51992a0>:0x007facf520fec8>

Extracted source (around line #1):
1: <%= form_for(@metafield) do |f| %>

模型“metafield.rb”如下所示:

class Metafield < ActiveRecord::Base
  attr_accessible :namespace, :key, :value, :value_type, :description, :id
end

控制器“metafields_controller.rb”定义了以下方法:

  # GET /metafields/1/edit
  def edit
    @metafield = ShopifyAPI::Metafield.find(params[:id])
  end

文件“/views/metafields/index.html.erb”如下所示:

<h1>Listing metafields</h1>

<table>
  <tr>
    <th>Namespace</th>
    <th>Key</th>
    <th>Value</th>
    <th>Value type</th>
    <th>Description</th>
    <th>Id</th>
    <th></th>
  </tr>

<% @metafields.each do |metafield| %>
  <tr>
    <td><%= metafield.namespace %></td>
    <td><%= metafield.key %></td>
    <td><%= metafield.value %></td>
    <td><%= metafield.value_type %></td>
    <td><%= metafield.description %></td>
    <td><%= metafield.id %></td>
    <td><%= link_to 'Edit', edit_metafield_path(metafield) %></td>
  </tr>
<% end %>
</table>

文件“/views/metafields/edit.html.erb”如下所示:

<h1>Editing metafield</h1>

<%= render 'form' %>

<%= link_to 'Back', metafields_path %>

文件“/views/metafields/_form.html.erb”看起来像这样(其中,显然,第 1 行抛出错误......):

<%= form_for(@metafield) do |f| %>
  <% if @metafield.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@metafield.errors.count, "error") %> prohibited this metafield from being saved:</h2>

      <ul>
      <% @metafield.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :namespace %><br />
    <%= f.text_field :namespace %>
  </div>
  <div class="field">
    <%= f.label :key %><br />
    <%= f.text_field :key %>
  </div>
  <div class="field">
    <%= f.label :value %><br />
    <%= f.text_field :value %>
  </div>
  <div class="field">
    <%= f.label :value_type %><br />
    <%= f.text_field :value_type %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

提前致谢!

4

1 回答 1

0

It`s not a good idea to nest forms in view partials as you do it in _form.html.erb.

<%= form_for(@metafield) do |f| %>
  <% if @metafield.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@metafield.errors.count, "error") %> prohibited this metafield from being saved:</h2>

      <ul>
      <% @metafield.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

You can try to use this helper:

<%= error_messages_for 'metafield' %>

instead of using form_for() helper.

UPDATE

If you wish to use form_for() helper it will be correct to define your Metafield model this way:

class ShopifyAPI::Metafield < ActiveRecord::Base
{
}

Since the form_for() helper tries to figure out a path to the view that will be rendered on the form submission. This path is returned by shopify_api_metafield_path() helper which is undefined method from the error message.

Regarding error_messages_for() helper, it will not be translated into HTML form tag and can be used inside a view partial without nesting HTML forms. See more here

于 2013-04-06T06:11:40.173 回答