1

I'm attempting to add custom fields like first name, last name, avatar_url, and bio to my Spree user.

I added the fields to the table successfully:

class AddFieldsToSpreeUser < ActiveRecord::Migration[6.0]
  def change
    add_column :spree_users, :first_name, :string
    add_column :spree_users, :last_name, :string
    add_column :spree_users, :bio, :text
    add_column :spree_users, :avatar_url, :string
  end
end

I checked and the schema was updated according to plan:

  create_table "spree_users", id: :serial, force: :cascade do |t|
    t.string "encrypted_password", limit: 128
    t.string "password_salt", limit: 128
    t.string "email"
    t.string "remember_token"
    t.string "persistence_token"
    t.string "reset_password_token"
    t.string "perishable_token"
    t.integer "sign_in_count", default: 0, null: false
    t.integer "failed_attempts", default: 0, null: false
    t.datetime "last_request_at"
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "login"
    t.integer "ship_address_id"
    t.integer "bill_address_id"
    t.string "authentication_token"
    t.string "unlock_token"
    t.datetime "locked_at"
    t.datetime "reset_password_sent_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "spree_api_key", limit: 48
    t.datetime "remember_created_at"
    t.datetime "deleted_at"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "first_name"
    t.string "last_name"
    t.text "bio"
    t.string "avatar_url"
    t.index ["bill_address_id"], name: "index_spree_users_on_bill_address_id"
    t.index ["deleted_at"], name: "index_spree_users_on_deleted_at"
    t.index ["email"], name: "email_idx_unique", unique: true
    t.index ["ship_address_id"], name: "index_spree_users_on_ship_address_id"
    t.index ["spree_api_key"], name: "index_spree_users_on_spree_api_key"
  end

I added the new attributes to the Spree initializer:

Spree::PermittedAttributes.user_attributes << [:first_name]
Spree::PermittedAttributes.user_attributes << [:last_name]
Spree::PermittedAttributes.user_attributes << [:bio]
Spree::PermittedAttributes.user_attributes << [:avatar_url]

When I try to create a new user in rails c the new attributes come up as part of the model.

I have the following form to attempt to update the user:

<%= simple_form_for current_spree_user, url: spree.spree_user_registration_path(current_spree_user) do |f| %>
  <%= f.object.errors.full_messages.join(", ") if f.object.errors.any? %>

  <div class="row text-left">
    <div class="col-sm-6 py-3">
      <%= f.label "First Name" %>
      <%= f.text_field :first_name, class: "form-control", required: true, autofocus: true, input_html: { autocomplete: "fname" }  %>
    </div> <!-- col -->

    <div class="col-sm-6 py-3">
      <%= f.label "Last Name" %>
      <%= f.text_field :last_name, class: "form-control", input_html: { autocomplete: "lname" }  %>
    </div> <!-- col -->

    <div class="col-12 py-3">
      <%= f.label "Author Bio" %>
      <%= f.text_area :bio, class: "form-control" %>
    </div> <!-- col -->

    <div class="col-12 py-3">
      <%= f.label "Profile Image URL" %>
      <%= f.text_field :avatar_url, class: "form-control", input_html: { autocomplete: "lname" }  %>
    </div> <!-- col -->
  </div> <!-- row -->

  <div class="form-actions text-center">
    <%= f.button :submit, "Let's Go", class: "btn blue" %>
  </div>
<% end %>

When I hit submit, I get a routing error:

ActionController::UnknownFormat

I have tried specifying method: :patch but it gets the same result. Can anyone see where I'm going wrong here?

Liz
  • 1,369
  • 2
  • 26
  • 61
  • I'm not familiar with Spree specifically. What controller handles the form request? Can we see that controller? – Kaom Te Aug 23 '20 at 20:05
  • @KaomTe That's the problem with Spree. The controller is in the gem, not something you can edit. I'm flying blind. – Liz Aug 24 '20 at 02:00

0 Answers0