rails + simpleform: selected does not set to be enum's value - ruby-on-rails

I have a enum definition for my User model.
class User < ActiveRecord::Base
enum program_of_study: [
:program_of_study_unknown, :program_of_study_cs, :program_of_study_ceg,
:program_of_study_is, :program_of_study_science,
:program_of_study_engineering, :program_of_study_fass,
:program_of_study_business, :program_of_study_others
]
end
And in simple_form I have:
<%= simple_form_for(locals[:user], wrapper: :horizontal_form, html: {class: 'form-horizontal'},
url: {action: (locals[:is_new] ? 'create' : 'update')}) do |f| %>
<%= f.error_notification %>
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: locals[:user].program_of_study %>
<%= f.button :submit, class: 'btn-success' %>
<% end %>
However, it seems that although the program_of_study of a user is 'program_of_study_science'(by checking in rails console), when I render the form, the shown select element still has 'program_of_study_unknown' as the displayed one. The correct one was not selected.

Instead of the enum I used the keys and it seems to work, have you tried that:
collection: User.program_of_studies.keys
I didn't have to specify a selected option. My code looks like this:
input :status, as: :select, collection: Venue.statuses.keys, include_blank: false

My solution in the end
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: User.program_of_studies[locals[:user].program_of_study] %>

Related

Rails simple_form element with data-... attributes for stimulus?

I am trying to hook up a stimulus controller to a form element which is inside a form generated by the simple form gem. Whatever I do - the corresponding "data-controller" attribute doesn't appear in the final rendered page (and hence, the controller won't react)...
Here's the code in question:
<%= simple_form_for book do |f| %>
<%= render 'shared/form_errors', form: f %>
...
<%= f.association :book_format, label: false, 'data-controller': 'book-format-select' %>
...
< % end %>
I also tried
<%= f.association :book_format, label: false, data_controller: 'book-format-select' %>
and
<%= f.association :book_format, label: false, html: {data-controller: 'book-format-select' } %>
None of that works - the output is always the same:
<select class="select required" name="book[book_format_id]" id="book_book_format_id">
Not sure what I am doing wrong?
You need to use input_html key
It is also possible to pass any html attribute straight to the input, by using the :input_html option
<%= f.association :book_format, label: false, input_html: { data: { controller: 'book-format-select' } } %>

Translating form_for syntax to simple_form syntax

I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).

Rails: remove the label from a simple_form_for collection association

I am trying to remove the label that is auto included when we use '.association' of the simple_form_for. But, regardless of what I do, the title and its <hr> continue to be displayed.
I tried:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: false %>
and
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: "" %>
But it keeps showing :/
What can I do to remove it?
You can try this:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label_method: "" %>

rails forms updating a db record

I would like to rewrite a form which is used to update a record on a database.
I want to update the form so that the form input does not show the record, as
the record is outputted by the line
<%= q.object.content %>.
I want the
form input not to display the record, and I want that the record is updated
when the input field is edited, and is not edited when it is left blank.
I am new at working with forms and don't know the best way to achieve this.
Can anyone provide any help on achieving this ? Below is the current form. Any help would be appreciated.
<%= semantic_form_for #bunchOfThings do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :aThing, #aThing do |q| %>
<%= q.object.content %>
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
<% end %>
<% end %>
<%= f.action :submit , label: t('Some Text'), button_html: { class: 'btn btn-primary' } %>
<% end %>
You can manually set the default value of a field to an empty string by changing this line:
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
To this:
<%= q.input :content, label: "A Thing: #{q.object.content}", input_html: {value:''} %>
You would also need to filter out blank fields on the backend within the update controller method. Something like this:
def update
filtered_params = permitted_record_params
filtered_params.keep_if{|k,v| !v.blank? }
record.update(filtered_params)
...
end
Where of course the permitted_record_params method returns your permitted params hash.

Rails 4 dropdown menu pulling from database

I am trying to implement a dropdown menu to display all the names I have in my database. I tried unsuccessfully the following code:
<%= form_for #airline, remote: true do |f| %>
<%= f.select :name, [#airlines.names] %>
<%= f.submit %>
<% end %>
Controller:
def index
#airlines = Airline.all
#airline = Airline.new
end
I assume the solution is quite straight forward but I couldn't find it.
This should do
<%= f.select(:name, #airlines.collect { |airline| [airline.name,airline.id] }, {:include_blank => 'Choose 1'},:class=>"class_name") %>

Resources