column value for input into database? - ruby-on-rails

How do I create a column in my table that is an input field?
i cant find it anywhere,
I'm looking to have the column for a price that is set at nil/empty, whatever.
Then the user inputs a price which is validated against a reserver price column, if successful it is added to cart.
It's the first part I'm having bad trouble with as my cart is set up already

You'll need a migration that adds a string field do your database. Check out the Rails guide for information on migrations, but it's essentially this:
add_column :my_table, :my_field, :string
Then it will automatically be cast to a string when retrieved from the database. Once you have that field, you can use the form helpers:
<%= form_for #my_model do |f| %>
<%= f.text_field :my_field %>
<%= f.submit %>
<% end %>

You can use text_field like this : http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag.

Related

JSONB: store_model gem. Modify content on posting data

I'm using StoreModel gem to wrap my JSON-backed DB column setting with an ActiveModel-like classes. This is my model simplified model
class Entity < ApplicationRecord
attribute :settings, Setting.to_type # settings is a jsonb datatype in the database
end
class Setting
include StoreModel::Model
attribute :setting1, :boolean
attribute :setting2, :boolean
attribute :setting3, :boolean
end
For the form I have the following
<%= form_for #entity do |f| %>
<%= fields_for :settings, #entity.settings do |ff| %>
<%= ff.check_box :setting1 %>
<%= ff.check_box :setting2 %>
<% end %
<% end %>
For an existing record, this would overwrite all values in the settings attribute of my Entity model, hence it would set setting3 no null (which is not passed by the form / params)! How can I submit values to keep existing values and just modify ones I submit.
It was discussed in the GitHub issue earlier, TLDR it's not possible out–of–the–box, but there is a workaround.

How to save data to multiple tables in Rails

I am using Devise for authentication in my app and got a model named as "Users". I created an another model for Company informations. I generated the migration with references and everything is okay.
I want to add user informations to users table and company informations to companies table in same registration view.
Also both users and companies tables got the same field as "name".
I tried to specify it as ;
<%= f.text_field :company["name"], autocomplete: "company_name" %>
<%= f.text_field :company[:name], autocomplete: "company_name" %>
<%= f.text_field :companies[:name], autocomplete: "company_name" %>
And i got the errors ;
undefined method `' for #<User:0x00007fd5588cb588>
no implicit conversion of Symbol into Integer
I think somehow i should override devise_controller but i want to learn the general practice for these kind of situations.
Sum: I want to save data to different tables which has same column names in database.
Assuming user belongs_to company and you want to save user and their associated company from one form submit, you can use fields_for for this
<%= f.fields_for :company do |cf| %>
<%= cf.text_field :name %>
<% end %>
This will require accepts_nested_attributes_for :company in User.

Rails tutorial - displaying username instead of user_id

In the Rails Tutorial there is a great chapter on creating a toy app with users and microposts. However, when editing the microposts, I can only edit user_id which is a numeric value, not user name. Is there a simple way to enforce displaying user's name instead of user's id in the app?
I've looked app/views/microposts/_form.html.erb and it says:
<div class="field">
<%= f.label :user_id %><br>
<%= f.number_field :user_id %>
</div>
What should I change to be able to select the users by name instead of the id?
Try using a select helper rather than a number_field.
<%= f.collection_select(:user_id, #users, :id, :first_name) %>
In your controller, you'd need the following line (or something similar):
#users = User.all
If you want to display each user's full name, you'd need to create a method in user.rb to concatenate first and last names, like so:
def fullname
fullname = "#{last_name}, #{first_name}"
end
Your select would then use the method name, like this:
<%= f.collection_select(:user_id, #users, :id, :fullname) %>
You should probably take some time to read up on all the different form helpers.
The feature you're looking for is called a collection_select. http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
f.collection_select :user_id, User.all, :id, :username

Submitting default value in text form Ruby on Rails

I'm developing a simple rails app where I want to plot some stock charts. The problem is that when I start my server and load localhost the default value/ticker symbol is not loading which means that I have to type in a ticker in my form for it to work.
I found this thread where I learnt how to write a default value in my form/view, like so:
<%= form_for :find_it do |f| %>
Ticker symbol: <%= f.text_field :string, :value => "JPM" %></br>
<%= f.submit "Find" %>
<% end %>
and that's all fine, but it does not submit the value by default.
So how do I go about fixing this and what is the best practice?
In your input field you have list your attribute as a string, while that is the type, it most likely isn't the actual name of the attribute you wish to save "JPM". So you should change
<%= f.text_field :string, :value => "JPM" %>
to
<%= f.text_field :attribute_name, :value => "JPM" %>
If I copy and paste your form into a Rails app on my machine it does display a text field populated with "JPM", which I believe is correct.
When you hit submit the form will post to a create action with params containing:
"find_it"=>{
"string"=>"JPM"
}
Another thing I noticed is that you have f.text_field :string. This should be the name of your attribute, rather than the type (i'm assuming that you don't have a field called string).

Simple_Form display validation error messages next to different input field

I have a form that allows the user to search for existing records to populate an association. Each "Booking" belongs to a "Customer". So the form allows you type the customer's name, it does a search automatically, and you click the customer you want. The input field you're typing into should display the customer's name, but for the form to work, I set the customer_id in a hidden input field.
I'm using the simple_form gem. Does anybody know if I can display the validation errors for the customer_id next to the text input field that displays the customer's name? The customer_id is required in the model, so I need the form to tell the user that if they leave it blank.
View code (simplified -- there's some JavaScript that handles searching when you type into the customer text box, and that sets the value in the hidden field to the customer's id when you make a selection):
<%= simple_form_for #booking do |f| %>
<%= f.hidden_field :customer_id, id: "customer_id" %>
<%= f.input :customer, required: true,
input_html: { value: #booking.customer_name } %>
<% end %>
I eventually found out about the append block in simple_form (buried in a pull request, not documented anywhere else that I could find). Basically, you can use that to append whatever markup you want to your f.input. I did the following, which is pretty hacky, but it does what I need it to do:
<%= f.input :customer, required: true,
wrapper_html: { class: ("error" if #booking.errors[:customer_id].any?) } do %>
<%= f.input_field :customer, value: #booking.customer_name %>
<%= f.error :customer_id %>
<% end %>
First, I had to conditionally add the class "error" to the wrapper if there were any errors on the related field, then I used the append block to render out the input field, followed by the errors for the related field from the model. Hope this helps someone in the future!

Resources