Ruby on Rails Formatastic ovverride attribute - ruby-on-rails

I want to override an attribute in my Rails Project but the problem is, that in my form (Formtastic) it still displays the database value and not the one from the getter. Do I have to write a custom getter and setter with annother name and save the value to the first attribute or is there a better way to do this?

If I understand you correctly you want to have a field in your form with a specific value, that is different from the actual value in the database. In this case you could pass the value option like this:
f.input :field, :input_html => { :value => "value" }

Related

rails association populate field from another table

Hoping this will be a straight forward question, but is anyone able to let me know the best way of populating a hidden field based on a value on another table.
I currently have 2 tables - table_numbers which has the following fields - id and value(decimal), and table_records which has an amount field.
On the form to add a new record I have the following to add a value to it
= f.association :table_number, :collection => table_numbers.order('value ASC'), :label_method => :value, :prompt => "Select a value", :label => "value"
At the moment this is populating the number_id on the records table, but displaying the value on the form when adding a record. What I would like is to get the value as well to be able to run a calculation on the value and amount.
What would be the best way to do this? Update the line above or do I need to add extra code?
Thanks
You can perfom calucaltions in before_save callback in model. If you want to display calculations, use helper and show it.

rails set selection value to null rather than blank

I have the following code which allows a user to select a currency rather than inheriting one.
%dd= user_field(#user, :remuneration_currency, :type => :select, :collection => REMUNERATION_CURRENCIES)
I need to allow people to set this field back to NULL, which means that the user will pick up their account's default currency. However, if I change the code to this...
%dd= user_field(#user, :remuneration_currency, :type => :select, :collection => REMUNERATION_CURRENCIES, :blank => 'Company Default')
then it sets the value to blank (as it says on the tin) rather than NULL in the database. How do I set the 'Company Default' value to be NULL rather than blank.
Your best option is to use the gem nilify_blanks which does exactly what you are looking for:
In Rails when saving a model from a form and values are not provided
by the user, an empty string is recorded to the database instead of a
NULL as many would prefer (mixing blanks and NULLs can become
confusing). This plugin allows you to specify a list of attributes (or
exceptions from all the attributes) that will be converted to nil if
they are blank before a model is saved.

How do I properly use a select box with ruby on rails?

I have a select box for the event types of an event. event belongs to event_type and event_type has many events. Here's what I have for the select box:
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :id, :name, #event.id), :placeholder => 'Select an event type' %>
But, the problem is that when I submit it, it submits the id of the event type and not the name of it. So, how would I make it submit the name rather than the id? If you need to see the any more code than just tell me, thanks
The second parameter to options_from_collection_for_select is the value that will be submitted with the form. You have :id, so change it to :name.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
(but this seems like a strange thing to do - typically you would store the event type ID.)
You can use the id after the submit to load the event type again in your controller post action like this:
selected_type = EventType.find(params[:event_type]
It is also a good practice to keep database calls to the controller, so please put the EventType.all statement in there and pass it as local or class variable like you did with event.
If you really want to pass the name in your form instead of the id, you can replace the :id part in your call to something more like this options_from_collection_for_select(#event_types, :name, :name, #event.event_type.name). Keep in mind that this value should be unique!
The method works like this:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
So the first parameter contains all the options, the second defines the value within those option objects which are put into the value field of the HTML option (which is being submitted by the form), the third defines the text which is displayed to the user and the final parameter defines the value of the selected entry (in case you are editing an entry for example). For the last parameter, you need to use the events' event_type id, or in your case, the name because you set the value of your HTML tag to it.
Use pages like ApiDock or the Rails tutorials to get examples for some of these methods.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
You should pass name in the Value method, if you want to pass the name,
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :name, :name, #event.id), :placeholder => 'Select an event type' %>
Here is the doc for options_from_collection_for_select

How do I use a check_box_tag for value that isn't a boolean?

I have a field in my database called property_types which has a string value and the values can be "Condo" or "Single Family Home" or "Commercial". I want to display each property type in the user's search form with a check box next to each property type so the user can select which property types should be included in their search results.
But if the property_type field is not a boolean and it has several possible values how do I do this using check_box_tag which can only have a true or false value?
EDIT:
I added this checkbox code to properties/index where the search form is and the search results are displayed:
<%= check_box_tag(:condo, value = "1", checked = true) %>
In properties_controller I added this:
#properties = #properties.where(:property_type => "Condo") if params[:condo].present?
Ofcourse it doesn't work because I haven't got a clue what I'm doing but is this along the right lines or not?
Well, looking at you requirements i would suggest you to leave the values at true and false, and on your controller, when you persist the object, you check all possibilities that are checked (true) and put you project_types attribute together.
project_types += 'condo&' if params[:condo]
project_types += 'commercial&' if params[:commercial]
...
Of course the & i put in the examples are just for demonstration, as i don't know how you would create this string.
EDIT:
Well, maybe you should refactor your model. I believe it would be easier if the project_types attribute didn't exist, and instead, you had several boolean attributes named after your strings. Then you can search like this:
#properties.where(:condo => params[:condo], :commercial => params[:commercial])
And it would be easier to understand the code.

Setting Rails Checkbox from Another Model

I have a checkbox that belongs to "Foo" class. I have another "Preferences" class that sets the default for what that checkbox should be.
I tried using
f.check_box :email_preference, :value => preferences.email_preference
but it doesn't work. I use this page to do new record creation as well as edit, so obviously for new records I would want to take the preferences.email_preference setting as a default, then for editing the record use the foo.email_preference. Any suggestions?
Try Following
check_box_tag :preference, :email_preference, :value => preferences.email_preference
Try this:
value = #foo.new_record? ? preferences.email_preference : #foo.email_preference
f.check_box :email_preference, :value => value
You're doing this in the wrong place. The view shouldn't care about the preferences class. When a new Foo is created, it should set the value of :email_preference on the object, and then the view will simply display the result of this.
I forget the name of the constructor method on ActiveRecord classes, or if there is a callback to leverage here. I'll look it up.

Resources