Hello, I teach rails, I decided to write an account store for myself. There are two models: Account, Image. Ideally, what would be used when creating an account through form_for
I could register a list of images for an account that are then displayed on the page (link has_many
, belogs_to
). But for now, I’d need to specify one image for my account when creating it.
accounts_controller:
def create render json: params @account = Account.new(require_params) if @account.save redirect_to @account else render :new end end
_form
<%= form_for(@account) do |f| %> <%= f.label :title %><br> <%= f.text_field :title %><br> <%= f.label :desc, 'descriprion'%><br> <%= f.text_area :desc %><br> <%= f.label :price %><br> <%= f.text_field :price %><br> <%= f.label :data %><br> <%= f.text_area :data %><br> <%= fields_for @account.images.build do |g| %> <%= g.label :link %> <%= g.text_field :link %> <%end%> <%= f.submit %> <%end%>
schema
create_table "accounts", force: :cascade do |t| t.string "title" t.text "desc" t.text "data" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "price" end create_table "images", force: :cascade do |t| t.string "link" t.integer "account_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "images", ["account_id"], name: "index_images_on_account_id"
class Account < ActiveRecord::Base validates :title, presence: true, length: {minimum: 10} validates :desc, presence: true, length: {minimum: 10} validates :data, presence: true, length: {minimum: 4} #validates :price, presence: true has_many :images end
- Escobar