This guide covers how to add editable attributes to models in Solidus.
We will cover an example of adding a new attribute to the Spree:Taxon
model
to allow for an additional text area.
Columns can be added to Solidus database tables using standard Rails migrations standard Rails migrations .
If you wanted to add a more_information
attribute, you could run the
following command:
rails generate migration AddMoreInformationToTaxons
This would create a migration file starting with a timestamp
db/migrate/20190925192026_add_more_information_to_taxons.rb
. Then update the
file with the following contents to add a new text type column
more_information
to the table:
class AddMoreInformationToTaxons < ActiveRecord::Migration[5.2]
def change
add_column :spree_taxons, :more_information, :text
end
end
After you are done editing your migration run the following command to add the column to the database:
rails db:migrate
We have a complete guide on customizing the admin panel , but will go over it here briefly for our example. While that guide covers template replacement and using the Deface gem, we'll just cover the Deface approach. This allows changing parts of the views without overriding them entirely.
To add a new form field for a custom attribute to the taxon form, we would
want to extend app/views/spree/admin/taxons/_form.html.erb
from the
Solidus backend. We can do that with Deface by creating a file
app/overrides/spree/admin/taxons/_form/add_fields.html.erb.deface
.
Below is an example of adding a new field to this form for a more_information
field.
<!-- insert_bottom '[data-hook=admin_inside_taxon_form]' -->
<div class="col-10">
<%= f.field_container :more_information do %>
<%= f.label :more_information %><br />
<%= f.text_area :more_information, class: 'fullwidth', rows: 6 %>
<% end %>
</div>
If you try to submit the form with the new attribute field, you may notice it will not save to the database. This is due to many models having a list of permitted attributes. Models with a defined list of permitted attributes are defined in https://github.com/solidusio/solidus/blob/master/core/lib/spree/permitted_attributes.rb.
To extend the list for a model, we can add the following line to an initializer, for
example in config/initializers/spree.rb
:
Spree::PermittedAttributes.taxon_attributes << [:more_information]
This tells Solidus that it's okay to accept values being saved to your custom attributes. If you were going to do this for multiple attributes, you can comma separate them.
After this has been added, you will need to reboot your Rails server. Now you can use the form in the admin panel to manage your custom attribute.
Solidus is an open source platform supported by the community. We encourage everyone using Solidus to contribute back to the documentation and the code.
If you’re interested in contributing to the docs, get started with the contributing guidelines. If you see something that needs fixing and can’t do it yourself, please send us an email.