Use Dropdown selection to call specific action in controller - ruby-on-rails

I have a has_many through association for three models. One model has individual tests that can be administered. The second model is the name of a group of those tests. (The group name of triathlon will have associations to multiple records in the individual test model).
I want the user to be able to select a group name from a Dropdown
and when they do it passes a param to a specific action in a controller that then is tested through the association model.
I can get the form to look correctly, but not pass parameters to a specific action and then return to the same page.
I have searched and tried for hours to no avail. What’s the correct Cr form_for syntax (or something entirely different.)
Below is my current code
<% form_for #labwork, :url => {:action => "groupcreate"} do |f| %>
<%= f.form_group :lab_id, class: 'row' do |f| %>
<%= f.label "Group", class: 'control-label col-md-2' %>
<div class='col-12 col-md-10'>
<%= f.collection_select :lab_id, Labgroup.all, :id, :name, {prompt: "Choose a lab group "}, class: "form-control" %>
<%= f.error_messages %>
</div>
<% end %>
<div class="d-flex">
<div class="ml-auto">
<%= f.submit class: 'btn btn-primary' %>
<%= link_to "Cancel", labworks_path, class: 'btn btn-outline-default' %>
</div>
I also have a def creategroup in my labworks controller.

Related

Rails Form - Error when deselecting on a collection_select

I have a bare bones rails form. I have minimal javascript for adding and removing the category fields. I don't want it to be fancy so a rails specific solution is what I'm looking for.
The form is for Posts with a has_and_belongs_to_many relationship to Categories.
<%= form.collection_check_boxes :category_ids, Category.all, :id, :name, {prompt: "None", include_hidden: false}, {multiple: true} %>
<% #posts.categories.each do |category| %>
<%= form.fields_for :categories, category do |c_subform| %>
<div>
<%= f.hidden_field :_destroy %>
</div>
<div class="row clearfix">
<%= f.label :name, "Category name", class: 'form-label' %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<% if #posts.new_record? %>
<div>
<%= link_to "Remove Category", '#', class: "remove_fields", style: "color: red" %>
<%= link_to_add_fields "Add Category", form, :categories %>
</div>
<% end %>
<% end %>
<% end %>
I added the ability to select multiple categories and there is another fieldset below allowing the user to create new categories. The issue is while editing a post, if I deselect one of the previously selected categories (in this case, the one with ID of 3), on save I get the error: ActiveRecord::RecordNotFound Couldn't find Category with ID=3 for Post with ID=23.
I would love for the update action to just delete the posts_categories record connecting Post 23 and Category 3 without deleting Category 3.
I want to add that the form worked fine until I decided to add the multi-select functionality by making it checkboxes that could be deselected and also required the category names be unique.

create multiple nested resources with a single form

I'm creating user permissions for my project, so I created a model called "profile", each "profile" record will represent a module of my proyect, so a "user" has many "profiles", I need to create a form In which I can create multiple profiles to the user and modify all those records in a single form. The form would be like this , but I would like to know how I could do to create multiple nested records in a single form and that each one is independent since each record will have fields like the name of the module that identify it, for example "products"
So far I have something like this, but it only shows me a single field:
<div class="form-group">
<%= f.fields_for :profiles do |profile| %>
<%= perfil.hidden_field :Module, value: "Products" %>
<%= f.label :Bajas,"Bajas:", class: "control-label" %>
<%= f.check_box :Bajas %>
<%end%>
</div>
You should use form-group of div inside #user and change the div like this:
<%= form_for #user do |user| %>
<div class="form-group">
<%= f.fields_for :permission, #user.permission do |profile| %>
<%= perfil.hidden_field :Module, value: "Products" %>
<%= f.label :Bajas,"Bajas:", class: "control-label" %>
<%= f.check_box :Bajas %>
<%end%>
</div>
<% end %>

Rails - dynamically build nested object inside nested object partials (Cocoon)

Currently, I'm building a Medium-like blog service which users can add article parts(text, image, youtube...etc) that composite the article body.
Article model has many ArticlePart model and to create nested form dynamically, I'm using cocoon gem.
Here's my current code.
_form.html.erb
<%= link_to_add_association("Add New Article Part", f, :article_parts,{:data => {"association-insertion-node" => "#article" }}) %>
<ul class="sortable">
<div id="article">
<%= f.fields_for :article_parts %>
</div>
</ul>
_article_part_fields.html.erb
<li class="nested-fields">
<h2>Text Part</h2>
<%= f.hidden_field :position %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= link_to_remove_association("Delete part", f) %>
<h2>Insert New Article Part Here</h2>
</li>
Above works fine but what I want to do is to display "link_to_add_association" where "Insert New Article Part Here" blocks in _article_part_fields.html.erb so that users can add new article part anywhere they want to.
I tried to do it as below by passing parent object form to nested object form, but this code causes stack level too deep error.
<li class="nested-fields">
<h2>Text Parts</h2>
<%= f.hidden_field :position, :class => "position" %>
<%= f.label :title %>
<%= f.text_field :title, :class => "fld-name required" %>
<%= link_to_remove_association("Delete part", f) %>
<h2>Insert New Part Here</h2>
<%= link_to_add_association("商品", parent_form, :article_parts,{:render_options => {:locals => {:parent_form => parent_form}}, :data => {"association-insertion-node" => "#article" }}, :class => 'btn btn-ctrl btn-lg') %>
</li>
Any help appreciated to get this working, or suggest another way to achieve this.
Thanks in advance!

Passing Custom Form Values to a Custom Controller Action

In Rails, I have a custom controller action that needs to accept some parameters from a form:
def update_ordid
# Get the active exchange
#exchange = Exchange.find(params[:id])
# Decide which order ID field to update
active_order_field = params[:ordfld]
# Save the order ID
order_id = params[:ordid]
if active_order_field == 1 then
#exchange.order_id_1 = order_id
else
#exchange.order_id_2 = order_id
end
#active_exchange.save
respond_with(#exchange)
end
Because these parameters aren't actual data fields in the exchange table, I would typically invoke the action by using a link such as:
link_to "Update Order ID", update_ordid_exchange(ordfld: value_from_form, ordid: value_from_form), :method => :post
Because in this case the value of these parameters needs to be populated by user input, I created the following form to pass the data:
<%= form_for(#exchange, url: update_ordid_exchange_path) do |f| %>
<div class="field">
<%= f.label :ordid, "Order ID" %><br>
<%= f.text_field :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%f.hidden_field :ordfld, :value => "1" %>
<% else %>
<%f.hidden_field :ordfld, :value => "2" %>
<% end %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
When I attempt to render this form, I receive the error: undefined method `ordid' for #
When researching this issue, I found that I could be able to do this by changing the text_field line to:
<%= f.text_field_tag :ordid, class: "form-control" %>
While this resolves the initial error, it throws a new error: undefined method `text_field_tag' for #
Any ideas as to what I'm doing wrong?
You cannot use form_for since your form elements doesn't represent the attributes of a model. use form_tag instead
<%= form_tag(update_ordid_exchange_path, :method => :patch) do%>
<div class="field">
<%= label_tag "Order ID" %><br>
<%= text_field_tag :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%= hidden_field_tag :ordfld, "1" %>
<% else %>
<%= hidden_field_tag :ordfld, "2" %>
<% end %>
<div class="actions">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<%end%>
Documentation here
form_tag vs form_for
hidden_field_tag
I'm a bit new to this myself, but I believe it's because you're calling "text_field_tag" from a form_for instead of form_tag builder object. Try just leaving off the form for object as such:
<%= text_field_tag :ordid, class: "form-control" %>

rails form using multiple tables

I am trying to create a form that updates a table. I want to have a dropdown button that pulls information from one table called "manufacturer" and once the form is submitted, stores the data in the "reviews" table.
This is my form:
<%= form_for(#review) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field" align= "center">
<h3>Select bat</h3>
<%= f.collection_select :bat_id, Manufacturer, include_blank: true %>
<h3>What do you like about this bat?</h3>
<%= f.text_area :pros, placeholder: "Enter what you like..." %>
<h3>What do you not like about this bat?</h3>
<%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br>
</div>
<div align="center">
<%= f.submit "Add Review", class: "btn btn-large btn-info" %>
</div>
<% end %>
When you look at this line- <%= f.collection_select :bat_id, Manufacturer, include_blank: true %> I believe :bat_id tells the the form where to send the input from the user. (in this case to the :bat_id parameter in the review table)
How would I tell the form to select the dropdown options from the manufacturer table?
Update:
Would I need to add anything in the controller?
Look at the documentation http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_select
collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
# method: the field in your form's object
# collection: the collection of active records to display as options
# value_method: which method call in items of the collection to get the displayed value
# text_method: which method call in items of the collection to get the id value
So in your case:
<%= f.collection_select :bat_id, Manufacturer.all, :id, :name, include_blank: true %>
Given that Manufacturer model has a name field, otherwise you need to adapt

Resources