1
votes

As part of a customisation I'm doing for a Shopify site, I'm trying to write a tool that allows only a specific metafield to be edited.

On the index page, I'll be listing various collections, products, pages and blog articles which are associated with a specific metafield I want the user to be able to edit via a form.

... for the life of me, I just can't see what I've missed!! Any help in resolving this is greatly appreciated :)

So ... here we go! When the user clicks 'edit' (which should take them to a form to edit the particular metafield for that collection, product, page or blog article), I'm getting this error in the browser :

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| %>

The model "metafield.rb" looks like this:

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

The controller "metafields_controller.rb" has the following method defined:

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

The file "/views/metafields/index.html.erb" looks like this:

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

The file "/views/metafields/edit.html.erb" looks like this:

<h1>Editing metafield</h1>

<%= render 'form' %>

<%= link_to 'Back', metafields_path %>

The file "/views/metafields/_form.html.erb" looks like this (of which, apparently, line #1 is throwing the error...):

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

Thanks in advance!

1

1 Answers

0
votes

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