Simple_form and association - ruby-on-rails

I have this form:
<%= simple_form_for #request, html: {class: 'form-horizontal' } do |f| %>
<%= f.input :initiator, label: 'initiator' %>
<%= f.association :department, collection: Department.all, value_method: :id, label: 'Назначить на отдел' %>
<%= f.association :user, collection: User.all, label_method: :email, label: 'Ответственный' %>
<%= f.input :comment, label: 'comment' %>
<%= f.input :sla, label: 'SLA' %>
<%= f.button :submit, label: 'Создать', class: "btn btn-primary" %>
<% end %>
How can I make the association:
If I choose "Department 1" from: Department, the choice of the :user will only users who belong to this department. (When you open the drop-down list were only people from the Department 1, not Users.all) What parameters I have to pass the rails?

I doubt if it is possible only using simple_form
Try http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/
I would advice against loading all users in DOM, use an ajax action to filter users if they are many

Related

Passing params to a simpleforms f.association in rails

I have many equipments that belongs to an Office
From the list of Offices, I put this link to pass the id to the form of a new equipment
<%= link_to "", new_equipment_path(office: params[:id])" %>
The generated url appears to be ok, passes the correct id of the clicked office
/equipments/new?office=6
And I check with an open cell like this and I see the number id (6 for example)
<%= f.input :observations, input_html: { value: params[:office]} %>
The problem is when I try with the real cell that is f.association, is just in blank, manually I can pick a number (1,2,3,4,5,6, etc) but It's not putting the number that I pass in the params, I try as hidden and other options and no luck
<%= f.association :office, label_method: :id, value_method: :id, input_html: { value: params[:office] } %>
#show.html.erb (offices)
<% #offices.each do |office| %>
<tr>
<td><%= office.name %></td>
<td><%= link_to "Add new equipment", new_equipment_path(office:params[:id])%></td>
</tr>
<% end %>
form.html.erb (new equipment)
<%= simple_form_for(#equipment) do |f| %>
<%= f.association :office, label_method: :id, value_method: :id, input_html: { value: params[:office] } %>
<%= f.input :observations, input_html: { value: params[:office]}%>
<% end %>
First I try without simpleforms and work without a problem
<%= form.hidden_field :office_id, value: params['office'] %>
The problem was that simpleforms does not take input_html: { value: params[:office] in f.association, I change for this and it work
<%= f.input :office_id, value_method: :id, label_method: :id, input_html: { value: params[:office] }, as: :hidden %>

Checking simple_form f.input value before submitting the form

I have this form:
<%= simple_form_for(#student) do |f| %>
<%= f.input :first_name, label: "Nombre" %>
<%= f.input :hours_studying, as: :select, collection: [1, 2],
prompt: 'Horas' %>
<%= f.input :library_id, as: :select, collection: #libraries %>
<div class="text-center">
<%= f.button :submit, "Regístrame!", class: "btn button rounded-pill
p-3 my-4 fs-18 shadow" %>
</div>
<% end %>
Libraries have a country and a city, how could I add two fields to the form to check the country and the city and filter the libraries accordingly?

Show Shipping Price When Selected In Form

I have a Shipping_method model. A shipping method has an attribute price_in_cents.
When user is viewing the cart form i want them to be able to select which shipping method they want. Which they can.
However. I want the price_in_cents to be displayed next to each shipping method. How do i do that?
<%= simple_form_for :cart, url: user_carts_path(current_user), method: :post do |f|%>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :product_id, value: product.id %>
<%= f.hidden_field :outfit_id, value: params[:outfit_id] %>
<%= f.hidden_field :price, value: product.price %>
<%= f.input :quantity, collection: 1..10, selected: 1 %>
<%= f.input :size_id, collection: sizes %>
<%= f.input :shipping_method_id, collection: shipping %>
<%= f.button :submit, value: "Buy Now" %>
<% end %>
Try this :
<%= f.input :shipping_method_id, collection: shipping, label_method:
:price_in_cents %>

simple_form using same attribute multiple times on same form via associations

Is it possible to have the same association on one form such as this?
<%= f.association :classifications, label: "Recipient", collection: #classifications, input_html: {class: "parent_recipients"}, as: :collection_select %>
<%= f.association :classifications, label: "Sub-recipient", collection: #sub_classifications, input_html: {class: "sub_recipients"}, as: :collection_select %>
So that the user can select from these two select boxes and have both entries saved into the database?
edit:
<%= f.simple_fields_for :classifications do |c| %>
<%= f.input :classification_ids, label: "Recipient", collection: #cat, as: :collection_select %>
<%= f.input :classification_ids, label: "Sub-recipient", collection: #sub, as: :collection_select %>
<% end %>
You can use simple forms nested model support:
<%= f.simple_fields_for :classifications do |c| %>
<%= c.input :classification_attribute %>
<% end %>
Make sure your parent model allows nested attributes to be accepted for this association:
accepts_nested_attributes_for :classifications

Rails: Issue with simple_form

I've built a simple_form form in my rails app and things went just fine:
<%= simple_form_for([#folha, #servico], html: {class: 'well form-horizontal'}) do |f| %>
<%= f.association :pessoa, label: 'Funcionário' %>
<%= f.input :funcao, label: 'Função',collection: #funcoes %>
<%= f.input :modulos, label: 'Módulos', input_html: {class: 'span4'} %>
<%= f.input :valor, label: 'Valor por hora', as: :string ,input_html: {class: 'span1'} %>
<%= f.input :horas, as: :string, input_html: {class: 'span1'} %>
<%= f.button :submit, 'Incluir', class: 'btn btn-primary' %>
<% end %>
To change the order list on the dropdown for the f.association i've overwritten the default .all method in Pessoa.rb:
def self.all
order :nome
end
Then i got this error when trying to render my view:
wrong number of arguments (1 for 0)
Extracted source (around line #5):
2: <h1>Preencher Pagamentos - Folha <%= "#{#folha.mes}/#{#folha.ano}" %> <small> <%= #folha.obs %> </small> </h1>
3: </div>
4: <%= simple_form_for([#folha, #servico], html: {class: 'well form-horizontal'}) do |f| %>
5: <%= f.association :pessoa, label: 'Funcionário' %>
6: <%= f.input :funcao, label: 'Função',collection: #funcoes %>
7: <%= f.input :modulos, label: 'Módulos', input_html: {class: 'span4'} %>
I think its better find a way to order the list in the view. But im very curious about what
is going on...
You should not override those kind of methods in first place.
Here is how to do it with simple_form
f.association : pessoa, :collection => Pessoa.order(:nome).all
https://github.com/plataformatec/simple_form/#associations
You can use default_scope as described here.
Use this macro in your model to set a default scope for all operations on the model.
In your case default_scope order(:nome).

Resources