using enum to select gender is not working - ruby-on-rails

I would appreciate any help. I'm using enum to select the gender between male and female. But is not working, is returning female but not male, it appears empty. And when I go to the edit section it doesn't show male or female as selected.
When I go the rails console and I type gender.enum? it returns true or false.
That's my Customer model
class Customer < ApplicationRecord
enum gender: [:female, :male]
end
That's what I have in the _form.html.erb
<%= radio_button_tag(:gender, "male") %>
<%= label_tag(:gender_male, "Male") %>
<%= radio_button_tag(:gender, "female") %>
<%= label_tag(:gender_female, "Female") %>
The gender was in boolean before, then I changed it to integer in order to use enum
class ChangeGenderData < ActiveRecord::Migration[5.2]
def change
change_column :customers, :gender, :integer
end
end
Here is an image of what appears

I solve the problem using this code.
<%= form.collection_radio_buttons :gender, Customer.genders, :first, :first %>

Related

Rails form search or create

In a Product model where it as an associated brand, I would like to have an input field where I can select an existing brand or add a new one.
In the form I have:
<%= f.fields_for :brand do |b| %>
<div class="form-group">
<%= b.label :name, t('brand.one') %>
<%= b.select :name, options_from_collection_for_select(Brand.all, :name, :name, product.brand.name), { include_blank: true}, class: '0select2-find-or-create' %>
</div>
<% end %>
and in the models:
Product:
belongs_to :brand
accepts_nested_attributes_for :brand, limit: 1
Brand:
has_many :products
But every time I change the Brand of a product it will change that brand's (by brand id) name, instead of changing the id.
Also for the part of creating a new brand I will try to use select2 with the tags option. Any other suggestion?
I think it might be because of your options_from_collection_for_select(Brand.all, :name, :name, product.brand.name)
I believe for the second parameter you want id and not :name.
Reference:http://apidock.com/rails/v4.2.1/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select

How can I pass value to the model with calculating in rails

For example, I have a model, it has 'id_number(integer)' and 'date(string)', I have:
<%= f.label :date %>
<%= f.text_field :date %>
<%= f.submit %>
I want to get this date(string) and save it as 'id_number' in integer to the model. How can I achieve this by ruby on rails?
You could use a before_save callback to handle this.
class YourModelClass < ActiveRecord::Base
before_save :date_to_id_number
def date_to_id_number
#code that converts date to id_number
end
end

how to get the reference?

please help solve the problem.
There is a table users:
id :integer, autoincrement
name :string
gender_id :integer
There is a table genders:
id :integer, autoincrement
gender_name :string
model User:
class User < ActiveRecord::Base
belongs_to :gender
end
model Gender:
class Gender < ActiveRecord::Base
has_many :users
end
I need the gendername. I try to do so:
<% #users.each do |user| %>
<div class="col-xs-6"><%= user.name %></div>
<div class="col-xs-2"><%= user.gender_id %></div>
<% end %>
the result is output number, but I need to gendername
my column 'gender_id' contain zero value. This problem has been

Show value in Active Admin

I am trying to show the value of an attribute via its id with formtastic. I have two models hat are setup like this
class Membership < ActiveRecord::Base
has_many :members
attr_accessible :membership_type
end
class Member < ActiveRecord::Base
belongs_to :membership
accepts_nested_attributes_for :membership
attr_accessible :membership_id, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end
My index view is set up like this so far
index do
column :forename
column :middlename
column :surname
column :house_no
column :house_name
column :street
column :town
column :postcode
column :home_tel
column :mobile_tel
column :work_tel
column :email
column :membership
default_actions
end
but the value for the membership is output as
#<Membership:0x007f2c2064c370>
How would i get the actual value so it will say "Student" for example
Normally i would do something like this i guess
<% #members.each do |m| %>
<%= m.forname%>
<%= m.surname%>
<% m.memberships.each do |s| %>
<%= s.membership_type%>
<% end %>
but cant figure it out within formtastic
Thanks
ActiveAdmin tries its best to find a display name for your objects using a list of possible attributes:
:display_name
:full_name
:name
:username
:login
:title
:email
To ensure your Membership object has a recognizable name throughout the system you can add one of those attributes as a method to Membership:
class Membership < ActiveRecord::Base
has_many :members
attr_accessible :membership_type
def display_name
membership_type
end
end
Alternatively, you can do like MichaƂ Albrycht suggests and configure views to perform custom rendering for the membership column.
Try this:
column 'Membership type' do |member|
"#{member.membership.membership_type}".html_safe
end
column 'Other members' do |member|
member.membership.members.each() do |m|
"#{m.forname} #{m.surname}<br/>".html_safe
end
end
Thanks #Cmaresh. Problem is solved. This is the commit link where i solved the problem in my project. Any one can see the source code and can easily understand how to solve the problem.

Fields_for setting up form not working correcty with accepts_nested_attributes

I'm sure that this is just a lack of experience on my part so bear with me.
I have a model MenuItem that has a Price. Different item types (menu_items, products, events) can have prices.
I have set it up as follows:
class MenuItem < ActiveRecord::Base
...
has_one :price, :as => :pricable
accepts_nested_attributes_for :price
attr_accessible :price_attributes
...
end
class Price < ActiveRecord::Base
belongs_to :pricable, :polymorphic => true
end
The Price object has a price value which is a decimal(8,2) on Mysql5.
In my form:
<%= form_tag "/menus/save" do %>
...
<% menu_header_form.menu_items.each do |item| %>
<div><%=item['header'] %></div>
<%=text_field :menu_item, :header, :index=>item.id, :value=>item.header %>
<%=text_field :menu_item, :sort, :index=>item.id, :value=>item.sort, :size => 2 %>
<% item.fields_for :price do |menu_item_price| %>
<%= menu_item_price.text_field :price %>
<% end %>
<% end %>
and am getting the following error:
undefined method `fields_for' for #<MenuItem:0x007fec8d9be138>
How would I iterate through to get the price value? Would the way that my models are set up mean that those menu_items would have a price record associated with them by default(even empty / null values)?
thx
you need fields_for :price , not item.fields_for
for a more complete example, take a look at the pattern here
All menu items would have a null value for price unless you explicitly give them one which you could do in a before_save callback if you chose

Resources