There is a string array segment defined in psql. Here is the db migrate file:
t.string :segment, array: true, default: []
When a value for segment is entered, the value is put within {} and is saved into the psql correctly. Here is a value in postgres:
{#6CF511,#EDF511,#F51111}
But when this value is displayed in view with the code below:
<%= f.input :segment, :label => ('Seg Range Color Choices')' %>
But the value displayed is not correct:
["#6CF511", "#EDF511", "#F51111"]
Every value is appended " before and after. Also the [] instead of {} is used. The following jquery code is used to filled out the value:
$('#dashboard_detail_segment').val("<%=rpt_detail.segment%>");
In debug, rpt_detail.segment = array[3]. rpt_detail.segment.join(',') can be used to turn the array into a string of "#6CF511, #EDF511, #F511112" and this may be one way to display. My question is that is there other natural way to display the segment in Rails view?
Use the raw method so that Rails will not html escape the output.
$('#dashboard_detail_segment').val("<%= raw rpt_detail.segment %>");
Related
Using globalize gem to manage translations with autocomplete, there is a situation where a number of hooks need to be properly set. Note: this does not use hstore AFAIK. I have not managed to find a way to do so. The most productive set-up to date has
controller:
autocomplete :nation, :name, :full => true
Nation
translates :name
view
<%= autocomplete_field_tag 'nation_name', '', autocomplete_nation_name_itins_path, size: 35, :id_element => 'nation_id' %>
There is no inherent reference to nation_translations database table created by Globalize as of yet. As this image suggests, there is a problem:
Issue 1: The input remains binded to the base table's attribute value (I have not yet cleared them out as the Globalize gem suggests. Otherwise I'd be getting blanks). can is actually ready all values of canin master table... Typing in other locales, like cyrillic say Канада has naturally no effect as that value is not part of the Nation table.
What is interesting is that the drop-down values are being populated by Rails automatically, extracting the translation values of what is input.
Issue 2: I'd rather pass the parameter 'nation_id' which is naturally part of the nation_translations table with the form data. although I can append , :extra_data => [:nation_id] to the controller it is not being submitted (example in cyrillic where the input is given without any autocomplete)
{"utf8"=>"✓", "nation_name"=>"Канада", "commit"=>"..."}
Rails.logger.info :extra_data returns:
extra_data
Now the second issue can be overcome because a query like
Nation::Translation.where('name = ?', "Канада").pluck('nation_id')
returns a proper result. But that point is moot if the autocomplete is not playing ball with the user's input.
How can this be configured to have user input autocomplete with the current local translations?
this does get solved by creating an entirely new class with attributes nation_id, name, locale and can thus be called symbolically.
The query call is not that straightforward however. As the gem suggests, the method need to be tweaked
def autocomplete_nation_name
term = params[:term].downcase
locale = params[:locale]
nationtranslations = Nationtranslation.where('locale = ? AND name LIKE ?', locale, "%#{term}%").order(:name).all
render :json => nationtranslations.map { |nationtranslation| {:id => nationtranslation.id, :label => nationtranslation.name, :value => nationetranslation.name} }
end
intervening on the action method itself provides all the leeway desired...
I have a form where I wish to perform a calculation on a date before displaying it. The model has a field for start, and a method adjusted_start and adjusted_finish which I want to display instead of the raw attribute.
I've tried:
f.input :start, as: time, label: false, minute_step: 5, value: openingtime.adjuted_start
However this is over ridden with the raw 'start' attribute. Is there any way to get it to display a different value than the one it has stored for start?
Thanks in advance.
I guess it should be input_html: { value: openingtime.adjuted_start }.
You need to wrap html attributes in input_html hash.
Check out doc.
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.
I am using simple_form and have a following sample tag:
<%= f.input :medical_conditions, :label=>false, :collection => medical_conditons, :as => :check_boxes%>
The collection holds about 100 checkboxes. However, when user only selects 1 or 2, everything is still getting saved to the database like this:
---
- ""
- ""
- ""
medical_conditions is a simple array in my application_helper
def medical_conditons
t = [
"Allergies/Hay Fever",
"Diabetes",
"Heart Surgery"]
return t
end
the medical_conditions field is a :string field.
What do I need to do so that only values that are selected are saved in comma separated manner.
It is not simple_form behavior. It is from Rails. See this: http://d.pr/6O2S
Try something like this in your controller (guessing at how you wrote your create/update methods)...
params[:medical_conditions].delete('') #this will remove the empty strings
#instance.update_attribute(:medical_conditions, params[:medical_conditions].join(','))
#or however you want to save the input, but the key is the .join(',') which will
#create a comma-separated string of only the selected values, which is exactly
#what you're looking for me thinks :-)
If that does the trick for you, I'd consider making a private helper method that formats the params for you so that you can use it in #create, #update, or wherever else you need it. This should keep things a little cleaner and more "rails way-ish" in your crud actions.
I am using simple form to make checkboxes as below:
module ApplicationHelper
def all_colors
t = [
"A",
"B",
"C",
"D"
]
return t
end
<%= f.input :colors, :label=>false, :collection => all_colors, :as => :check_boxes%>
When the user selects some checkboxes and submits the form, the data saved in databases also has non selected items empty location.
For example If user checkmarked B and D then data saved in DB will look like:
---
- ""
- B
- ""
- D
I want the data to be saved as CSV
At a glance it seems to me you might be passing extra commas in your input so when your user checkmarked B and D what got passed back in the request was- ,B,D,.
When you parse the input to save the extra commas were translated into empty values.
Check the request variables that were posted back by the form submit and strip away the extra commas.