I'm working a project that has Rails API for the back-end and Vue(Vuetify) for the front-end. There is a nice way of building a form using the <v-form></v-form>
tag, but I'm having an issue implementing a field within my form that has a field for JSON. I can easily have one for a string with:
<v-text-field
v-model="host"
label="Host"
solo-inverted
/>
I have an attribute participants, which is a nested array (json) that takes name and email. Been trying to find a way to have my vue form that can take an array.
Here is my database schema:
create_table "shows", force: :cascade do |t|
t.string "host", null: false
t.string "location", null: false
t.text "message"
t.json "participants", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
Tried to find a field for JSON object on a v-form, but can't find anything in the official docs.
There's no built-in Vuetify form field that automatically displays an object. In this case, you have to explicitly bind each object property to a form field/label.
For example, consider participants to be this object:
{
id: 'P1',
label: 'Famous Mathematicians',
names: [
{
first: 'Alan',
last: 'Turing'
},
{
first: 'Isaac',
last: 'Newton'
}
]
}
You could use Vue's string interpolation for participants.label and v-for to map participants.names like this:
<v-form>
<h3>{{ participants.label }}</h3>
<v-col v-for="name of participants.names">
<v-text-field v-model="name.first" label="First name" />
<v-text-field v-model="name.last" label="Last name" />
</v-col>
</v-form>
Related
I have a type that looks like this:
module Types
class LeadType < Types::BaseObject
graphql_name 'Lead'
description 'A lead data query selection'
field :first_name, String, null: true
field :last_name, String, null: true
field :email, String, null: true
field :gender, String, null: true
field :errors, [String], null: true
field :success, String, null: true
field :lead, Types::LeadType, null: true
field :home_data, GraphQL::Types::JSON, null: false
end
end
Note how I have field :home_data, GraphQL::Types::JSON, null: false
Now, when pulling my query data, it's a raw JSON like so:
{
"success"=>true,
"lead"=>
{
"home"=>
{
"1"=>
{
"roof"=>"GRAVEL",
"garage"=>"Yes",
"dead_bolt"=>"No",
"effective"=>"2022-08-25",
"sprinkler"=>"No"
}
},
"first_name"=>"John",
"last_name"=>"Doe",
"email"=>"john#doe.com",
"phone"=>"5599999777",
"gender"=>"Male"
}
}
Now I am able to query my data without the field:home_data, GraphQL::Types::JSON
But when I add the homeData to playground I have this error:
Now, I need the field :home_data, GraphQL::Types::JSON, null: false to display the:
"home"=>
{
"1"=>
{
"roof"=>"GRAVEL",
"garage"=>"Yes",
"dead_bolt"=>"No",
"effective"=>"2022-08-25",
"sprinkler"=>"No"
}
}
How I am able to achieve this using GraphQL::Types::JSON?
null: false means the field is non-nullable. This means that the field will never be nil. If the implementation returns nil, GraphQL-Ruby will return an error to the client
So if your request contains homeData field - your data must contain home_data JSON. But it doesn't. You have home key, not home_data
I'm trying to disable a field inside a select input.
There is no problem for the standard field:
(this disable the field 'one')
= f.input_field :job, collection: %w[one two three], disabled: 1, include_blank: 'Select one', required: true
What i would like to do now is to disable the blank field after a user selected an other field. I tryed this:
= f.input_field :job, collection: %w[one two three], disabled: 0, include_blank: 'Select one', required: true
But it didn't work... any idea ?
Thanks for your help and your time :).
To disable blank value we use disabled: "" and to select it before user selects anything we use selected: "". Notice that it works with prompt option instead of include_blank only.
= f.input_field :name, collection: %w[one two three], disabled: "", selected: "", prompt: 'Select one', required: true
I have a model:
class Client
include Mongoid::Document
field :name, type: String
field :age, type: Integer
index({ name: 1 }, { unique: true })
def self.list
self.all.as_json
end
end
When I call Client.list I get the following:
[{"client"=>{"_id"=>{"$oid"=>"58e91ccb9509d36cbaa8c79b"}, "name"=>"mark", "age"=>30}}]
What I am after is:
[{"_id"=>{"$oid"=>"58e91ccb9509d36cbaa8c79b"}, "name"=>"mark", "age"=>30}]
Which version of mongoid are you using ?
Check the documentation at:
https://docs.mongodb.com/ruby-driver/master/tutorials/6.1.0/mongoid-installation/
You can disable the include_root_in_json to remove the model name of the output json.
Can you try this solution?
This problem has me completely stumped. I'm still new at RoR and learning.
I have two tables: Countertops and Countmaterial. A user will select all the features for their countertop including the material type. The options for the material are listed in the Countmaterial table and are selection from a collection.
My question is once the selection is made and the Countertop created how do I display the name of the material type on the index page for Countertops instead of the countertype, which is an integer generated to match the name in the Countmaterial table?
I'd rather the index display "Granite" instead of "1", for example. "Granite" is listed in the Countmaterial table and when the user selects "Granite", it populates the Countertop table as "1" in the countertype column. Marble is a "2" and so on...
Here's my schema:
create_table "countertops", force: :cascade do |t|
t.string "size"
t.string "color"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "ZipCode"
t.string "countertype"
end
create_table "countmaterials", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "countertop_id"
end
My Countertop Controller for index:
def index
#countertops = Countertop.all
#countertops = Countertop.includes(:countmaterial).all
end
My index code:
<% #countertops.each do |countertop| %>
<tr>
<td><%= countertop.ZipCode %></td>
<td><%= countertop.countmaterial.name %></td>
Associations:
class Countertop < ActiveRecord::Base
has_one :countmaterial
end
class Countmaterial < ActiveRecord::Base
belongs_to :countertop
end
What do you folks think??
You're going to get confused with your specific model names; when naming models and controllers - keep it SUPER simple. One word...
#app/models/counter.rb
class Counter < ActiveRecord::Base
#columns id | type_id | material_id | size_id | color_id | zip_code| created_at | updated_at
belongs_to :type
belongs_to :material
belongs_to :size
belongs_to :color
delegate :name, to: :size, prefix: true
end
#app/models/option.rb
class Option < ActiveRecord::Base
#columns id | Type | name | created_at | updated_at
has_many :counters
end
#app/models/size.rb
class Size < Option
end
#app/models/type.rb
class Type < Option
end
#app/models/color.rb
class Color < Option
end
#app/models/material.rb
class Material / Option
end
This will give you the ability to do the following:
#config/routes.rb
resources :counters
#app/controllers/counters_controller.rb
class CountersController < ApplicationController
def index
#counters = Counter.all
end
end
#app/views/counters/index.html.erb
<% #counters.each do |counter| %>
<%= counter.size_name %>
<% end %>
To give you context on how this works, you need to know that Rails & Ruby are object orientated. This might not mean a lot, but it's vitally important when developing apps with them.
Object orientated programming is a pattern which puts the object at the center of the code. When you understand how this works, nothing will ever be the same...
In "traditional" programming, you work with user flow. This is known as event driven programming, and although works well for standard apps, it does not suit a Ruby/Rails environment.
Web apps have the capacity to handle so much more data / functionality that it makes perfect sense to treat everything as an object.
Thus, whenever you deal with Ruby, you have to think about everything from the perspective of the objects you're trying to CRUD (Create Read Update Destroy).
This is why your CounterTop model is a little sketchy - what's the object you're trying to invoke?
Once you see that the object sits at the core of how Rails works, you'll be able to construct everything around it, as above.
I have a model
class News < ActiveRecord::Base
serialize :types, Array
end
My Migration is
create_table "news" do |t|
t.string "types"
end
Usually we are giving
t.text "types"
But here only 3 types are there. So I need mysql to be allocated small space for types. So can I give
t.string "types"
or some limit
for an array serialize ?
To a first order, based on some limited tests, yes, you can use string instead of text as a schema type for serialized attributes.