Dear All,
View
<form align="center" name="gm" action="">
<label for="col1"><b>Name: </b></label>
<%= collection_select(#table, "gm", #pop1, "col1", "col1", :prompt => true) %>
<%= submit_tag value="Proceed-->"%>
<form name="sp" action="">
<label for="col2"><b>Class: </b></label>
<%= collection_select(#table, "sp", #pop2, "col2", "col2", :prompt => true) %><br><br>
<%= submit_tag value="Submit"%>
</form>
Here, these are relational collection_select. I need to populate the second collection_select once first collection_select was selected. But, once "Proceed" submit was processed, params[gm] was disabled. So I am unable to process "Submit" tag with both params[gm] and params[sp] for #table. Any idea to keep selected value in collection_select to remain after click "Proceed".
If I use
<%= collection_select(#table, "gm", #pop1, "col1", "col2", :prompt => true, :selected=> params[:gm]) %>
It works!
Related
How do I assign the selected option value:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
To a form's field, let's say: Voyage.given_season ?
Use the rails select field instead and do it like this
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
Hope this helps.
If you want your select input to accept multiple options, you can pass multiple: true
<%= f.select(:season_id, Season.all.collect {|m| [ m.name, m.id] }, class: "form-control select2-simple-dropdown", id: "list-markets", multiple: true) %>
https://aalvarez.me/posts/select2-with-simple-form-in-rails/
I have not been able to figure out if this is a simple_fields_for problem, a cocoon issue, or something else. If I have blundered with a </div> placement, I don't see it.
When the form first displays, it renders an input field for protocol name. The user can click buttons to add form elements of interest. This works fine and looks like this:
Here's how it looks after the user has clicked on each of the "Add an element" buttons:
The user can add 0..many of each of the elements. When they click on 'Save' it all works really well.
If there is a validation error in one of the fields, the form re-renders fine with one exception. Validation errors for the "Imaging Step" elements do not re-display at all. The other elements re-render and are highlighted as expected when validation fails.
Here's a pictorial example. The user fills out part of the form, having forgotten to select a "Sequence" and having forgotten to enter text for "Tip Description":
After clicking 'save' and failing validation, the form re-renders like this:
As you can see, the Imaging Step section has not been redrawn.
If I look at params in the context of the view, everything seems to be there. #protocol.errors looks right to me as well. Models seem OK too.
Here is a pastebin of the form code.
Here is a pastebin of _step_item_fields.html.erb.
Here is a pastebin of _tip_fields.html.erb.
Here is a pastebin of my Gemfile.
UPDATE:
if I build a step_item like this:
<div id="step_items">
<%= f.simple_fields_for :step_items, #protocol.step_items.build do |si| %>
<%= render 'step_item_fields', :f => si %>
<%end%>
</div>
The Imaging Step section is always drawn, but (obviously) does not populate when validation fails. This also confuses the feature of letting the user add/remove 0..many Imaging Steps.
I also tried:
<div id="step_items">
<%= f.simple_fields_for :step_items, #protocol.step_items.build(protocol_params) do |si| %>
<%= render 'step_item_fields', :f => si %>
<%end%>
</div>
... and other variants when protocol_params is available, but ran in to forbidden params issues.
UPDATE2:
I also tried building a hash with params for a step_item. I can use this here:
<div id="step_items">
<%= f.simple_fields_for :step_items, #protocol.step_items.build(some_ok_params) do |si| %>
<%= render 'step_item_fields', :f => si %>
<%end%>
</div>
... but only for a single step_item. Am not sure how to pass a hash of hashes reflecting the 0..many functionality. Also, building a step_item this way populates the form element correctly, but does not include the error highlighting styling. This is about the time I started wondering why the simpler solution was not working.
Since StepItem is a sub-type of the STI class Step, I think the error detection for StepItems was getting missed. My hack fix here is not pretty and admittedly brittle. Please post a better way if you have one.
I changed this in _form.html.erb:
...
<%if params["protocol"] && params["protocol"]["step_items_attributes"].present?%>
<%params["protocol"]["step_items_attributes"].each do |sia|%>
<%v = sia[1]%>
<div id="step_items">
<%= f.simple_fields_for :step_items, #protocol.step_items.build(:orientation_id => v["orientation_id"], :sequence_id => v["sequence_id"], :note => v["note"], :id => v["id"], :protocol_id => v["protocol_id"], :institution_id => v["institution_id"] ) do |si| %>
<%= render 'step_item_fields', :f => si%>
<% end %>
</div><!-- end step_items-->
<%end%>
<%else%>
...
I changed this in _step_item_fields.html.erb:
...
<div class="nested-fields">
<div class= "form-inputs">
<div class="row">
<div class="col-sm-2 text-right">Imaging Step:</div>
<div class="col-sm-2 drop_col ">
<%= f.collection_select :orientation_id, Orientation.where("institution_id=?",current_user.current_institution_id),:id,:name, {:prompt => "Pick an Orientation", }, {class: "form-control #{"dropdown_error" if f.object.orientation_id.blank? && (f.object.sequence_id.present? || f.object.note.present?)}"}%>
</div>
<div class="col-sm-2 drop_col ">
<%= f.collection_select :sequence_id, Sequence.where("institution_id=?",current_user.current_institution_id),:id,:name, {:prompt => "Pick a Sequence"}, {class: "form-control #{ "dropdown_error" if f.object.sequence_id.blank? && (f.object.orientation_id.present? || f.object.note.present?) }"}%>
</div>
</div>
<div class="row">
<div class="col-sm-2"></div>
<%= f.input :note, :wrapper_html =>{:class => 'col-sm-8'}, label: false, placeholder: 'You can put an optional note here.' , :input_html => {:size => 50}%>
<%= f.input :institution_id,:as => :hidden, :input_html => {:value=>current_user.current_institution_id} %>
<%= f.input :id,:as => :hidden %>
<div class="col-sm-2">
<%= link_to_remove_association "Remove Imaging Step", f , :class=>"btn btn-danger btn-xs placemid",title: "Click here to delete this imaging step.", data: {toggle: "tooltip", placement: "auto", animation: true, delay: {show: 700, hide: 100}}%>
</div>
</div>
</div>
<div class="col-sm-12"><hr></div>
</div>
...
And I added this to the correct stylesheet:
.dropdown_error{
color: red;
}
The form now displays StepItem errors correctly as seen here:
I want to display my dropdown form selected option value whenever an user edits the form. Right now it displays the first option (in this case a blank option).
Form
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]]), :include_blank => true, :selected => params[:condition]) %>
HTML Output
<select id="product_condition" name="product[condition]">
<option value=""></option>
<option value="Brand New">Brand New</option>
<option value="Pre-owned">Pre-owned</option>
</select>
JSON
{
id: 2,
condition: "Pre-owned",
}
Thanks.
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], :selected => f.object.condition), :include_blank => true) %>
Check the options_for_select documentation, and you will discover that the last parameter is the selected option.
options_for_select(container, selected = nil)
In your case
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], params[:condition]), :include_blank => true) %>
assuming params[:condition] contains the currently selected value, and the value matches the corresponding value in the select tag.
In other words, for "Pre-owned" to be selected, params[:condition] must contain "Pre-owned".
<%= f.select :condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], f.object.condition), {:include_blank => true} %>
<%= form.label :Hobbies%>
<%= form.select(:hobbies, options_for_select(%w[cricket football hockey
reading], :selected => form.object.hobbies )) %><br><br>
This form multiple drop down but user only select one and if you edit and show method show only one record.
Event saves the section id's in an array. But in the /events/1/edit view for a newly created event, the expected checkboxes are not checked. I'm guessing because the default for checkbox values is booleans.
Event.last.newsletters #=> ["108", "115", "116", "117", "118", ""]
I have a CRUD for Event. Each Event can belong to multiple Sections. I have this displayed as a collection of checkboxes with the simpleform gem.
<%= simple_form_for #event do |f| %>
...
<%= f.collection_check_boxes :newsletters, Section.all, :id, :name, :input_html => { :class => 'checkbox' } %>
This results in the following html:
<span><input id="event_newsletters_1" name="event[newsletters][]" type="checkbox" value="1" /><label class="collection_check_boxes" for="event_newsletters_1">Newsletter 1</label></span>
<span><input id="event_newsletters_2" name="event[newsletters][]" type="checkbox" value="2" /><label class="collection_check_boxes" for="event_newsletters_2">Newsletter 2</label></span>
etc. etc.
When I create a new event or edit an event, the newsletter values are saved properly in the model.
Try specify the param :checked:
<%= f.collection_check_boxes :newsletters,
Section.all,
:id,
:name,
:input_html => { :class => 'checkbox' },
:checked => #event.newsletters %>
Reference: how to preselect an association checkbox using simple_form
If you are using SimpleForm you can use this:
<%= f.input :newsletters, collection: Section.all, as: :check_boxes %>
here is my partial for writing a comment:
<div class="commentBox">
<%=form_for [commentable,Comment.new], :remote => true do |f|%>
<%=f.text_area :content, :placeholder=>"Write a comment...", :title=>"Write a comment..." %>
<%=f.hidden_field :parent_id%>
<br />
<%=f.submit "Add comment"%>
<%end%>
</div>
The issue with this is that the page is loading which the following:
....<textarea title="Write a comment..." rows="20" placeholder="Write a comment..." name="comment[content]" id="comment_content" cols="40"></textarea>
The problem with this is that my page can have several items on it that are commentable. So having a TEXTAREA with ID doesn't work...
What I'd like is for the textarea to have a class of comment_content, and then in the form I can add a hidden field with the recordID where the comment can live and then in the controller use that ID to insert into the DB. Does this sound like the right idea?
Thoughts?
You can always override default attributes such as id.
<%=f.text_area :content, :id => "text-1" %>
<%=f.text_area :content, :id => "text-2" %>