I would like to use concatenation in rails loop :
I tried :
collection: "Service.active.#{service.type}"
collection: Service.active. + "#{service.type}"
<% services.each do |service| %>
<div class="grid md:col-span-3">
<%= f.association :services, as: :check_boxes, collection: "Service.active.#{service.type}", input_html: { multiple: false } %>
</div>
<% end %>
If what you want is to dynamically invoke a method on Service you can use Object#public_send. If the method isn't public you can use send;
Service.active.public_send(service.type)
Related
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' } } %>
I got simple_form for testrun model with multiple checkboxes, that save an array of testcases in a model field
app/views/testruns/_form.html.erb
<%= simple_form_for #testrun do |f| %>
<%= f.input :testcase, as: :check_boxes,
collection: [["testcase1", :testcase1], ["testcase2", :testcase2], ... ]%>
<%= f.submit %>
<% end %>
It works fine, but from now I need to create another model called testcase. After submitting form, besides creating a new testrun instance, I need to create testcase instances which depends on every flag checked.
Any idea how can I do it?
You need to use accepts_nested_attributes_for and simple_fields_for. Assuming you have has_many :testcases in Testrun and the field name of Testcase is name, the below steps should put you in the right direction.
#app/models/testrun.rb
accepts_nested_attributes_for :testcases
#app/controllers/testrun_controller.rb
def new
#testrun = Testrun.new
#testrun.testcases.build
end
private
def testrun_params
params.require(:testrun).permit(:field1, :field2.., testcases_attrubtes: [name: []])
end
#app/views/testruns/_form.html.erb
<%= simple_form_for #testrun do |f| %>
<%= f.simple_fields_for :testcases do |testcase| %>
<%= testcase.input :name, as: :check_boxes,
collection: [["testcase1", :testcase1], ["testcase2", :testcase2], ... ]%>
<% end %>
<%= f.submit %>
<% end %>
How can I set default value in Rails select helper block?
<div class="field">
<label>Gender</label>
<%= f.select :gender, [], { prompt: 'Select gender', selected: 'Female' }, { :class => 'ui selection dropdown' } do %>
<% Subject.genders.keys.each do |c| %>
<%= content_tag(:option, value: c, class: 'item') do %>
<%= content_tag(:i, '', class: "#{c.downcase} icon") %>
<%= content_tag(:span, c) %>
<% end %>
<% end %>
<% end %>
</div>
I tried setting it with :selected option but it doesn't work.
I don't think if selected exist for f.select.
You can use options_for_select(values, value_selected)
Suggestion:
You can create a file named app/helpers/select_helper.rb. In this file, you create a function like this:
def subject_genders_values
Subject.genders.each do |c|
[c.value, c.value]
end
end
your function subject_genders_values can be re-used. And every time if you want a select box, you can create your function in this file.
Notice: add
include SelectHelper
in application_helper.rb
And your views:
<%= f.select :gender,options_for_select(subject_genders_values, 'Female') %>
The option Female will be selected if it's a part of the list.
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] %>
These are my params:
{"utf8"=>"✓", "authenticity_token"=>"0RYiIDDgmOk0gCDRkAgHvv+UIgp/BuU33CLThJXqOTE=",
"order"=>
{"operation_in_orders_attributes"=>
{"0"=>{"service_operation_id"=>"5"},
"1"=>{"service_operation_id"=>""},
"2"=>{"service_operation_id"=>"4"},
"3"=>{"service_operation_id"=>""},
"4"=>{"service_operation_id"=>""}},
"kontakt"=>"comment", "Car_id"=>"50"},
"commit"=>"Dodaj",
"car_id"=>"dw815gn"}
Order has many operation_in_orders
Order has many service_operations through OperationInOrder
OperationInOrder belongs to Order
OperationInOrder belongs to ServiceOperation
ServiceOperation has many operation_in_orders
ServiceOperation has many orders through OperationInOrder
My form:
<%= form_for #order, url: new_car_order_path(#car, #order), html: {class: "add_order"} do |r| %>
<%= r.label "Service", class: :add_order_label %>
<% 5.times do %>
<%= r.fields_for :operation_in_orders do |v| %>
<%= v.collection_select(:service_operation_id, ServiceOperation.all, :id, :nazwa,include_blank: true) %>
<!-- <%= v.text_field :order_id, value: #order.id, :style => "display:none" %> -->
<% end %>
<% end %>
<%= r.label "Kontakt", class: :add_order_label %>
<%= r.text_field :kontakt %>
<%= r.text_field :Car_id, value: #car.id, :style => "display:none" %>
<%= r.label " " %>
<%= r.submit "Add", class: "sub" %>
<%= link_to "Send",ordered_path(car_id: #car.id) , class: 'sub'%>
<% end %>
I have a form where I can choose five ServiceOperations at most to an order and save.
When I save, 5 new OperationInService objects/rows are made.
Is there a possibility to not create those join tables if corresponding field on form is blank?
For example:
I fill only 2 from 5 fields. I save only these two, not 5. Now I save nil values...
I have tried to validate in OperationInService model, but there was an error (rails do not recognize format in controller).
Any ideas?
Update the accepts_nested_form_for method call in Order model as below:
class Order < ActiveRecord::Base
has_many :operation_in_orders
accepts_nested_attributes_for :operation_in_orders, reject_if: proc { |attributes| attributes['service_operation_id'].blank? }
## ..
end
This way record for operation_in_orders would not be created if service_operation_id is blank.