Setting Rails Checkbox from Another Model - ruby-on-rails

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.

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.

Ruby on Rails Formatastic ovverride attribute

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" }

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.

Ruby on Rails: putting class with submit_tag

I was wondering why we have to add a nil when putting :class => "class_name"
<%= submit_tag nil, :class => "class_name" %>
but for this:
<%= f.submit class: "class-Name" %>
I don't need to add the nil
Thanks
<%= submit_tag("Update", :id=>"button", :class=>"Test", :name=>"submit") %>
First parameter is required and it would be value and they any parameter you want to specify, can be done in a hash like :key=>"value".
A look to the way that submit_tag method was implemented clearly answers your question.
def submit_tag(value = "Save changes", options = {})
options = options.stringify_keys
if disable_with = options.delete("disable_with")
options["data-disable-with"] = disable_with
end
if confirm = options.delete("confirm")
options["data-confirm"] = confirm
end
tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options)
end
It takes two arguments, the first is value which by default is "Save changes" and the second is a Hash of options. If you don't pass nil then it will assume that that's the value you want for the input.
Because they are two different methods...
The "submit" method doesn't take a caption because it can infer one from the form that the method is called on, and what object was used to build the form.
The "submit_tag" method is not called on a form object. It is used for more customized form building (more separated from your activerecord model, for example) and so the code can't infer a caption and must get a value as the first argument. All the "formelement_tag" methods (documented here, for example) are like this and can infer less based on your data model.
Obvious answer is that submit_tag and submit are simply different form helper methods that takes different arguments.
The _tag series of methods usually require a name parameter (otherwise they'd be fairly useless tags, so it's always the first argument instead of part of the hash. Because the submit helper is called as part of the form, Rails can assume the field's name property and can then make the options hash the first argument.

Resources