Rails simple-form with multiple instances of the same object - html

I am trying to create a rails form using simple form that uses nested resources. However, I want to be able to submit multiple instances of the associated resource. Example below will probably explain it better.
<div class="tab-pane active" id="reminder">
<%= simple_form_for #collection, html: {multipart: true}, url: collection_index_path do |m| %>
<%= render partial: "collection/tabs/reminder", locals: { :m => m } %>
</div>
-inside partial
<% 9.times do |j|%>
<div class="tab-pane" id="<%= j %>">
<%= m.simple_fields_for :reminder do |p| %>
<%= p.input :heading %>
<%= p.input :message %>
<% end %>
</div>
There is a tabbed pane in which the user can click through 9 tabs to set up to 9 reminders, all should be associated with a collection (collection model accepts nested attributes for reminder). However, the way I have it setup now, the controller only gets what was set in the last reminder in the params. Anyway ideas would be appreciated.

There must be some way to distinguish tabs before submitting to controller. And i think answer might be here.
i.e. it looks like this:
<% 9.times do |j|%>
<div class="tab-pane" id="<%= j %>">
<%= m.simple_fields_for :reminders do |p| %>
<%= p.input :heading %>
<%= p.input :message %>
<% end %>
</div>
<% end %>

Related

How can I filter a specific params key from params passed in a link_to?

I have an index where users can apply many different kinds of filters through checkboxes.
Each filter has an "X" link_to button that dismisses the filter, which basically makes a request to the same index page permiting the current params with the exception of the dismissed filter's param key.
For example:
<div class="form-group <%= 'hidden' unless params.dig(:q, "subscription_payment_type_eq").present? %>" id="subscription_payment_type_eq" data-toggle-target="objective">
<%= f.select :subscription_payment_type_eq, payment_types_selector_for_filter, { include_blank: "Forma de pago..."}, class: "form-control gray my-2 " %>
<% if params.dig(:q, "subscription_payment_type_eq").present? %>
<%= link_to admin_users_path(params: params.permit(q: [:extra_params, :address_province_eq, :subscription_status_eq, :created_at_gteq, :created_at_lteq])) do %>
<span class="dismiss-box glyphicon glyphicon-remove-circle">
<% end %>
<% end %>
</div>
<div class="form-group <%= 'hidden' unless params.dig(:q, "address_province_eq").present? %>" id="address_province_eq" data-toggle-target="objective">
<%= f.select :address_province_eq, user_provinces_selector_for_filter, { include_blank: "Provincia..."}, class: "form-control gray my-2 " %>
<% if params.dig(:q, "address_province_eq").present? %>
<%= link_to admin_users_path(params: params.permit(q: [:extra_params, :subscription_payment_type_eq, :subscription_status_eq, :created_at_gteq, :created_at_lteq])) do %>
<span class="dismiss-box glyphicon glyphicon-remove-circle">
<% end %>
<% end %>
</div>
The approach works, but it's very cumbersome to manually add the excepted param key to each new filter that is added to the index page.
Is there a way to pass params excluding the param key that belongs to the filter being dismissed?
I've tried the following but no params are passed:
<% if params.dig(:q, "subscription_payment_type_eq").present? %>
<%= link_to admin_users_path(params: params[:q].except(:subscription_payment_type_eq).permit) do %>
<span class="dismiss-box glyphicon glyphicon-remove-circle">
<% end %>
<% end %>
Also tried this but got undefined method 'exclude' for #<ActionController::Parameters:0x00007f6331b00dd8>
<%= link_to admin_users_path(params: params.exclude(:subscription_payment_type_eq).permit) do %>
Permit expects a list of args and since you're not sending any no params will be passed to your controller. I'm actually not sure how your calling permit without args is even working. Anyway maybe you can try it like this instead:
<%= link_to admin_users_path(params: params[:q].except(:subscription_payment_type_eq.permit!)
In the end we decided to implement it this way:
<%= link_to admin_users_path(
params: { q: params.require(:q).permit(params[:q].keys - ["address_province_eq"]) }
) do %>
We basically substract the filter's key from the current params when passing it in the link_to

Hash being saved to database but unable to render its values in Rails forms?

I added a new column to model Plan, named :per_unit_quantities_configuration which is a hash with min, max and step key/values.
t.jsonb "per_unit_quantities_configuration", default: {}
When I edit a Plan, the hash is being correctly saved to the DB (I can access each key/value from the console), but the forms are not displaying any of its values (the fields are empty).
I tried adding a store_accessor for the column in the Plan model, but it is not working:
store_accessor :per_unit_quantities_configuration, :min, :max, :step
Example of a simple_form html that does not display hash values:
<%= simple_form_for [:admin, #base_plan, #plan] do |f| %>
<% if f.object.base_plan.per_unit? %>
<div class="d-flex">
<%= f.simple_fields_for :per_unit_quantities_configuration do |fields| %>
<% if f.object.errors[:per_unit_quantities_configuration].any? %>
<%= f.error :per_unit_quantities_configuration, id: "per_unit_price_error", class: "invalid-feedback", error_prefix: "gato" %>
<% end %>
<%= fields.input :min %>
<%= fields.input :max %>
<%= fields.input :step %>
<% end %>
</div>
<% end %>
<%= f.button :submit, class: "pull-right" %>
<% end %>
What am I doing wrong?
since you setup store_accessor :per_unit_quantities_configuration then you can access directly 3 attributes min, max, step, so that you no need to wrap those attributes on simple_fields_for :per_unit_quantities_configuration and treat them as normal fields (that mean on controller you have to permit them as normal fields)
# view
<%= f.input :min %>
<%= f.input :max %>
<%= f.input :step %>
# controller
def plan_params
params.require(:plan).permit(:min,:max,:step)
end

Submitting multiple forms in one click

I was looking at the question below, which has a good answer for what I will ask, but I have a doubt. I cannot comment yet so that is why I am making another post.
How to submit multiple, duplicate forms from same page in Rails - preferably with one button
This is my view for the form
new.html.erb
<h1 class="page-title">Nuevo Precio</h1>
<hr class="title-division">
<div class="alta-form-container">
<%= form_tag "/precios" do %>
<% 2.times do %>
<%= render 'form', precio: #precio %>
<% end %>
<%= submit_tag "Submit", class: "btn btn-success" %>
<% end %>
<%= link_to 'Lista Precios Cliente', client_precios_path %> <br><%= link_to 'Ver', [#client, #precio] %> <br>
</div>
and my _form.html.erb
<div class="field">
<%= label_tag :precio, "Precio" %>
<%= text_field_tag "precios[][precio]" %>
</div>
<div class="field">
<%= text_field_tag "precios[][cant_acomodato]" %>
</div>
<div class="field">
<%= hidden_field_tag "precios[][client_id]" %>
</div>
When I submit the form I get an error that says
'No route matches [POST] "/precios"'
which I'm guessing is because on my new.html.erb I wrote form_tag "/precios" do
Any thoughts on what I should change or edit? thanks in advance
actually you have only one form here, because _form.html.erb will generate only inputs, that according to form below
<%= form_tag "/precios" do %>
that will generate
form action="/precios" method="post">
if you want leave it like that, you should add to your config/routes.rb
post '/precios', to: 'controller_name#method_name', as: :controller_name_method_name
your situation:
post '/precios', to: 'PreciosController#create', as: :precios_create
also check this
documentation

Some HTML for values of Hash keys

I am building a panel with a partial, but I'd like to customise the inside of the panel with some html. Is there any way to write some HTML for a hash value?
I have a custom partial _panel_builder.html.erb that I takes as argument pclass, heading, body, etc., that I would like to use like this :
(The below syntax is bad, but I don't really understand how I could do something nice..)
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
# For the body param, I'd like to be able to use some HTML with occasional <%=...%> for variables, like :
body: (%>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<%).html_safe,
collapsable: true %>
<% end %>
EDIT : An idea of what my _panel_builder partial looks like :
<%
collapsable ||= false
pclass ||= "default"
footer ||= false
%>
<div class="panel panel-<%= pclass %> <%= if collapsable then "panel-collapsable " end %>">
<div class="panel-heading">
<%= heading %>
<% if collapsable %>
<span class="pull-right"><i class="glyphicon glyphicon-chevron-down"></i></span>
<% end %>
</div>
<div class="panel-body <%= if collapsable then "collapse" end %>">
<%= body %>
</div>
<% if footer %>
<div class="panel-footer <%= if collapsable then "collapse" end %>">
<%= footer %>
</div>
<% end %>
</div>
Okay so I was actually looking for the capture helper :
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
body: capture do %>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<% end %>,
collapsible: true %>
<% end %>
Note : in some cases, I also want to pass a complex HTML block as the header of footer, so I can't just have a helper that takes a block.
Note2 : My panel is a HTML template made for generic data. I cannot pass it a Ruby object

Ruby localization

I have to localize a web site, everything is almost done, except I have ran into two problems. The code below is for a small input form, but where are the strings? There is a label for a text field and a button.
Secondly, I am receiving an error:
"undefined method `-' for "translation missing: lv.date.order":String".
Where do i have to create the translation? In the .yml file? If so, how?
Thanks in advance!
<%= form_for([:admin, #publisher]) do |f| %>
<% if #publisher.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#publisher.errors.count, t(:error)) %> <%=t(:prohibited_saved)%></h2>
<ul>
<% #publisher.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This error means that Rails cannot find the translation for date.order in current locale file.
You should have a lv.yml file inside config/locales with this content:
lv:
date:
order:
- :day
- :month
- :year
It will instruct rails to show dates in a format: day/month/year