How to limit character/word count on database string value in Sinatra/Active Record? - ruby

I have a column in my ActiveRecord database that I want to have a certain word limit.
Essentially, I've created a form that allows users to enter text(string). I want to limit the amount of characters that are allowed in that string.
#allposts = Post.limit(20)
This is what I have so far in the get method for the /current page that posts all of content. 20 = number of posts shown.
I also have a /new page where users will post new content.

You can limit the number of characters in a few different ways:
1.Defining the limit of the HTML field you create:
<input class="ip-input" id="ip" maxlength="15" name="ip" size="20" type="text" value="0.0.0.0" />
by changing the maxlength attribute. Example taken from here.
2.Using the validates option in the user model:
validates :attribute_you_want_to_limit, length: { maximum: 50 }
You can find more about this option here.
3.Putting a limit in the schema:
t.string :your_attribute, :limit => 20
The first option won't allow the user to input any more in the field, the second won't allow saving the object and the third option won't let the attribute get saved to the database.
I recommend the second option.
You can also use Javascript in a few different ways, here's a good explanation on how to.

Related

Can I access a specific field value from an object through accessing by the related name, in Django?

I'm making a vote app (Django rest framework), where users can post content and have it voted on through two options. The Vote modelVote model code has a ForeignKey field to the Post model, with the related name attribute "votes". The vote model also has a field named 'option', where choices 'option1' and 'option2' can be selected.
My goal is to count the total number of votes on a post, number of option1 votes and number of option2 votes.
I've been able to count the number of votes on a post through:
queryset = Post.objects.annotate(
votes_count=Count('votes', distinct=True),
)
Post views
I am unsure how to count the number of option1 and option2 votes, separately.

Oracle Apex Add IF Statement to set a value in a form

I am working on a system in Oracle Apex 22.1 where there is a Form, where the user enters an amount of money to be validated by the company. There are 3 fields which are a select list P3_REQUEST_TYPE, a text field P3_AMOUNT and a select list P3_TYPE_CURRENCY. What I want to do is that if the user selects the option "Payment Request" in P3_REQUEST_TYPE, and according to the amount entered in P3_AMOUNT and the type of currency in P3_TYPE_CURRENCY (USD, JPY), put the value "1" in the field P3_FG_RT for validation reasons.
I would like to know if there is a way to do it through a IF function since through dynamic actions I have not been able to make it work, I would greatly appreciate your help.
The amounts for the "1" to be placed are: if it is greater than or equal to 5,000 and they are USD, or if it is greater than or equal to 18,000 and they are JPY.
That looks like dynamic action, indeed.
its action would be "Set value"
type might be PL/SQL Function body:
RETURN CASE WHEN :P3_REQUEST_TYPE = 'Payment Request'
AND :P3_TYPE_CURRENCY IN ('USD', 'JPY')
THEN 1
END;
items to submit: P3_REQUEST_TYPE, P3_TYPE_CURRENCY
affected elements: item, P3_FG_RT
Though, as you said that these are Select List items, I kind of doubt that return values really are "Payment Request" or "JPY" - I presume that you actually return their codes, not descriptions so CASE expression might need to be changed.
The decision to use a dynamic action depends on the following question: is the value that needs to be set of importance before the page is submitted or does it only matter at submit time
#Littefoot explained how to do the dynamic action but if this value is only needed after the page is submitted, it is easier to set the value of page item P3_FG_RT with a a computation (process point After Submit).
One way to implement this computation is:
Type: Static Value
Static Value: 1
Server Side Condition: Expression
PL/SQL Expression:
:P3_REQUEST_TYPE = 'Payment Request' AND :P3_TYPE_CURRENCY IN ('USD', 'JPY')

How to create a form that contains data for multiple entities?

I've been trying to think of the best possible way to submit data for multiple entities in a single form.
My use case is that I have a table that contains multiple rows of different entities. I want the ability to go into 'edit' view where some of the columns will become textfields and allow for the value to be changed.
Example
Type | Name | Age
Animal | Eevee | 4
Animal | Sandy | 7
Human | Bob | 24
[Submit]
In edit mode they would be able to change the age for any of the entities. When the user hits save button for the entire list, I want the form to submit all the changes. The problem is that in the update method, I need to know which entities to update with which values
I was thinking of using name attribute to store the entity id of each entity on the input
<input name="{{$entity->id}} value="{{$entity->age}}"></input>
but Animals and Humans are stored in different DB tables. So in the update method of the controller I would have to take the name (which would be the id of the entity) and if it exists as a human
entityToUpdate = Human::find(id)
if entityToUpdate does not exist
entityToUpdate = Animal::find(id)
We use UUIDs in our DB so this would work but it doesn't feel right.
is there a way to group a couple inputs together in a form?
group1
type=animal
id = (Sandys ID)
age = 8
group2
type=human
id =(bobs ID)
age = 25
You could use array notation in your input names in order to get what you want on the server side:
#foreach($entities as $entity)
<input type="hidden" name="group[{{$loop->index}}][type]" value="{{$entity->type}}"/>
<input type="hidden" name="group[{{$loop->index}}][id] value="{{$entity->id}}"/>
<input type="text" name="group[{{$loop->index}}][age] value="{{$entity->age}}"/>
#endforeach
Then on the server side:
foreach(request()->get('group') as $entity) {
// $entity contains ['id'], ['type'], ['age']
}

Chosen field in active_admin

I have model User. One field in this model is gender. This field is integer. If user choose male I set 1, and if user chose female - I save this field as 0.
Also I have another models with the same problem in admin interface. For example in one of them I save day of week. It's very bad create some table with rows which will not change in future, but I doesn't have better solution
How I can make this better in active admin?
Get easy solution
column "Gender" do |user|
link_to (user.gender == 1) ? "Male" : "Female", "javascript:void(0)"
end

Best way to integrate values into enum (dictionary), then calculating based on user selection in form

Any guidance or pointing me to an example would be much appreciated (I can't formulate a good search term on the Googleplex).
I have a model using enums that i define in a dictionary and then render on the view with #Html.RadioButtonFor, etc.
Here is an example of my model:
public PaymentPlanList PaymentPlan { get; set; }
public enum PaymentPlanList
{
PaymentPlan_One,
PaymentPlan_Two,
}
public class PaymentPlanDictionary
{
public static readonly Dictionary<PaymentPlanList, string> paymentplanDictionary = new Dictionary<PaymentPlanList, string>
{
{ PaymentPlanList.PaymentPlan_One, "One full payment in advance (receive the lowest price)." },
{ PaymentPlanList.PaymentPlan_Two, "Two payments: first payment of 50% due up front, the balance of 50% due within 30 days (increases fee by $100)." },
};
static string ConvertPaymentPlan(PaymentPlanList paymentplanlist)
{
string name;
return (paymentplanDictionary.TryGetValue(paymentplanlist, out name))
? name : paymentplanlist.ToString();
}
static void Main()
{
Console.WriteLine(ConvertPaymentPlan(PaymentPlanList.PaymentPlan_One));
Console.WriteLine(ConvertPaymentPlan(PaymentPlanList.PaymentPlan_Two));
}
}
And, for completeness, this is my view related to the above:
<p>
#Html.RadioButtonFor(m => m.PaymentPlan, "PaymentPlan_One")
One full payment in advance (receive the lowest price).
</p>
<p>
#Html.RadioButtonFor(m => m.PaymentPlan, "PaymentPlan_Two")
Two payments: first payment 50% due up front, the balance of 50% due within 30 days (increases fee by $100).
</p>
This is a quote system I have users fill out. For this particular service, say I charge $1,000.00. This is the base price. Based on user input, this price will be changed, and I want to show that to the user. So, if the user selects the first option, the price remains unchanged. If the user selects the second option, the fee is increased by $100.00.
This changes exponentially, since there are more inputs that affect the price (if selected).
Ultimately, based on the user inputs, I need to calculate the total. I am rendering a view which will display the total. I was thinking of using some #{} blocks and if/else if statements to either a) show nothing if what was selected does not increase the total, or b) showing the additional amount (e.g., $100.00), and then later showing a total.
Something like (EDITING here for clarity):
Base service: $1,000.00
Addon service1: $100.00 (only if user selects "PaymentPlan_Two" for two payments of 50% each (from the PaymentPlanList enum), otherwise hidden (and no addition of the $100.00) if user selects "PaymentPan_One" and pays in full)
Addon service2: $0.00 (this is hidden and a $0.00 or no value since the user did not select anything from a separate enum, but the value of $100.00 would be added if selected, which would make the Total $1,200.00 if it were selected; ALTERNATIVELY, how could I handle if there were 3 or more items in the list? E.g., Choice_One is $0.00, Choice_Two is $100.00 and Choice_Three is $200.00)
TOTAL: $1,100.00
Thanks for any help.
let's see if I understand your requirements correctly:
The application needs to add prices to a base price, depending on
selection of Addon services.
These selections are from a Dictionary, which is based on an Enum
Therefore we're looking to store the price against the Enum to keep the data associations in one place.
It is possible to store a single value against an Enum:
public enum PaymentPlanList
{
PaymentPlan_One = 100,
PaymentPlan_Two = 200,
}
However, I don't think this would be flexible enough for our needs - Enums only allow integers, and are commonly used this way in bitwise operations (where the values are multiples of 2).
I think a better solution here might be to use a Model-View-View-Model (MVVM) which can contain the logic about which services are available, how much they cost, and which services are valid in combination with other services.
There's an ticket pricing example (which sounds similar in concept to the domain here) on the Knockout.js home page that re-calculates a travel ticket price on the client web-page based on a user selection.

Resources