Passing params to a simpleforms f.association in rails - ruby-on-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 %>

Related

Collection_Select Does Not Respond to Input_HTML: {Multiple: True}

I am attempting to create a feature where users can add an existing record, recipe, to a collection of records, menu. I am using collection_select with a simple_form to allow users to select multiple records from a list however, the form is not responding to the input_html: { multiple: true } option, which should allow users to select multiple values. The form is as below, please let me know if any other code would be helpful for context.
Form:
<%= simple_form_for #menu, local: true do |f| %>
<%= f.label :title, :class => "form-component-header" %>
<%= f.text_field :title, :class => "form-field" %>
<%= f.label :recipe_ids %>
<%= f.collection_select :recipe_ids, f.object.user.recipes, :id, :title, input_html: { multiple: true } %>
<%= f.submit :class => "form_button" %>
<% end %>
.permit(....recipe_ids: [])
You need to update the permitted parameters in your controller. Now that you are sending multiple selections the parameter needs to be marked as expecting an array.

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 and association

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

Select element, simple_form helper

I am trying to create an element in my form that uses simple form+bootstrap. The idea is to allow a user select a type of currency from a drop down.
Customer_currency to select from either USD- US Dollars, LRD - Liberian dollar among others.
I have used the following in my form
However, it is not working, all I see is a drop down (out of position) in my form with the options but no label.
How can i create a good select element with a label using simple form
<%= simple_form_for #customer, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
<%= f.input :payment_terms %>
<%= f.input :billing_address %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :mobile %>
<%= f.input :email %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
customers_path, :class => 'btn' %>
</div>
<% end %>
<%= f.input :customer_currency, :collection => [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
Add label_method and value_method to your select, means change:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
to:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>
Update: other solution
<%= f.input :customer_currency, as: :select, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>

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