I customised my collection radio buttons of my form, everything work when the value is a simple strings choice but i dont success to save the select value when its this type of field
Original field
<%= f.collection_radio_buttons(:negociateur, Nego.where(user: current_user), :first_name, :first_name) %>
What i tried to do
<div class="btn-group" data-toggle="buttons">
<% #negos.where(user: current_user).each do |n| %>
<label class="btn btn-danger" style="margin-right: 10px;">
<input id="bien_negociateur_true" name="bien[negociateur]" type="radio" autocomplete="off" value= '<% n.first_name %>' /* the problem is here */ /> <%= n.first_name %>
</label>
<% end %>
</div>
Related
I have a has_and_belongs_to_many relationship with my category and product models. The categories will display on the product form as checkboxes, but no matter what checkbox I select, it only effects the last checkbox on the list, so I am unable to select multiple categories.
products/_form.html.erb
...
<%= f.collection_check_boxes(:category_ids, Category.all, :id, :name, checked: product.categories.map(&:id)) do |b| %>
<%= b.check_box %> <%= b.label %>
<% end %>
...
products_controller.rb
def product_params
params.require(:product).permit(
...
category_ids: []
)
end
rendered html - from console
There are two inputs that have the "checked" value. This is because I started with a select option then switched to checkboxes. I am unable to un-select the first checkbox. If I check it, it un-checks the last on the list.
<div class="input-checkbox">
<input type="hidden" name="product[category_ids][]" value="">
<input type="checkbox" value="1" checked="checked" name="product[category_ids][]" id="product_category_ids_1">
<label for="product_category_ids_3">Test One</label>
<input type="checkbox" value="2" name="product[category_ids][]" id="product_category_ids_2">
<label for="product_category_ids_3">Test Two</label>
<input type="checkbox" value="3" checked="checked" name="product[category_ids][]" id="product_category_ids_3">
<label for="product_category_ids_3">Test Three</label>
</div>
Updated working code for multiple select_tag
I needed to get the form working, so I went to multiple select options. here is working code for Rails5. I would still like to get checkboxes working through.
<div class="cat-select">
<% array = Category.all.map { |category| [category.name, category.id] } %>
<%= select_tag 'product[category_ids][]', options_for_select(array, selected: #product.category_ids), multiple: true %>
</div>
I have a form created using Simple Form, as such
<%= simple_form_for #organisation do |f| %>
<div class="form-inputs">
<%= f.association :causes, as: :check_boxes %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
The page works fine when I use a browser, but when I try to check this with Capybara, such as:
check('organisation_cause_ids_1')
And have tried many variations of this e.g.
find(:xpath , '//*[#id="organisation_cause_ids_1"]').set(true)
find("organisation_cause_ids_1").check
These always give an error:
Failure/Error: check('organisation_cause_ids_1')
Capybara::ElementNotFound:
Unable to find checkbox "organisation_cause_ids_1"
The HTML generated by Simple Form is:
<div class="input check_boxes optional organisation_causes">
<label class="check_boxes optional">Causes</label>
<span class="checkbox">
<label for="organisation_cause_ids_1" name="organisation[cause_ids]">
<input class="check_boxes optional" id="organisation_cause_ids_1" name="organisation[cause_ids][]" type="checkbox" value="1" />Cause A</label>
</span>
<span class="checkbox">
<label for="organisation_cause_ids_2" name="organisation[cause_ids]">
<input class="check_boxes optional" id="organisation_cause_ids_2" name="organisation[cause_ids][]" type="checkbox" value="2" />Hunger</label>
</span>
...
Edit: The problem was due to the lazy loading of the 'Causes' I created with the factories. They weren't being created so the page had no checkboxes.
Try with this
find_by_id('organisation_cause_ids_1').find("checkbox[value='1']").select_option
or maybe with this
find(:css, ".check_boxes[value='1']").set(true)
I have a form that looks like this:
<%= form_tag site_update_admin_settings_path, :method => :put do %>
<fieldset>
<legend>
<h2>Default Meta Information</h2>
</legend>
<% for setting in #settings[:default_meta] %>
<div class="form-group">
<label><%= setting.name %></label>
<span class="help-block"><%= setting.description %></span>
<input type="text" class="form-control" name="settings[<%= setting.id %>]" value="<%= setting.value %>" />
</div>
<% end %>
</fieldset>
<fieldset>
<legend>
<h2>Social Media Information</h2>
</legend>
<% for setting in #settings[:social_media] %>
<div class="form-group">
<label><%= setting.name %></label>
<span class="help-block"><%= setting.description %></span>
<input type="text" class="form-control" name="settings[<%= setting.id %>]" value="<%= setting.value %>" />
</div>
<% end %>
</fieldset>
<fieldset>
<legend>
<h2>Analytics Information</h2>
</legend>
<% for setting in #settings[:analytics] %>
<div class="form-group">
<label><%= setting.name %></label>
<span class="help-block"><%= setting.description %></span>
<input type="text" class="form-control" name="settings[<%= setting.id %>]" value="<%= setting.value %>" />
</div>
<% end %>
</fieldset>
<%= submit_tag "Save Settings", class: "btn btn-primary" %>
<% end %>
The form ends up with about 10 fields on it that can be edited. The values for each field maps to the value column for each record in the database. An example of the database table looks like this:
| id | name | value |
-------------------------------------------------------
| 1 | site_title | Page Title |
| 2 | twitter_username | TwitterHandle |
So basically, the form fields turn into an array of settings that would be held in params[:settings]. The settings param holds key value pairs (id => value) for each field. The form only updates the value column for a specific row (determined by the id). I have the form submitting and creating the params[:settings] hash but haven't figured out how to get the controller to update the records. Thoughts? Please let me know if you need more information as well.
UPDATE:
In the params hash, settings looks like this:
"settings"=>{"1"=>"Website Title", "5"=>"TwitterHandle", "10"=>"FooBar"}
The key is the id for the settings table record and the value is what needs to go to the value column in the settings table.
params[:settings].each do |id, value|
Model.find(id).update_attributes(value: value)
end
I have a problem with the following code in the view:
<div class="btn-group" data-toggle="buttons">
<%= f.label :private, class: "btn btn-default" do %>
<%= f.check_box :private %> Private
<% end %>
</div>
Technically it works, but when the private field is initially true (i.e. checked) the button looks as if it was unchecked. Perhaps it is due the fact that check_box helper generates two inputs, one of which is hidden.
Could someone show an example implementation of Bootstrap checkboxes in the Rails view?
It turned out that Bootstrap scripts just do not update the state on page load. So I solved it with this JavaScript code snippet:
$("[data-toggle='buttons'] .btn").each(function(i, el) {
var $button = $(el);
var checked = $button.find("input[type='checkbox']").prop("checked");
if (checked) {
$button.addClass("active");
} else {
$button.removeClass("active");
}
});
Here is a working checkbox from one of my projects generated using Simple Form:
<%= f.input :cost, label: "Cost to access resource" %>
Which generates:
<div class="form-group boolean optional archive_resource_cost">
<input name="archive_resource[cost]" type="hidden" value="0" />
<label class="boolean optional checkbox" for="archive_resource_cost">
<input checked="checked" class="boolean optional" id="archive_resource_cost" name="archive_resource[cost]" type="checkbox" value="1" />
Cost to access resource
</label>
</div>
Is your :private model attribute definitely boolean?
Originally I had my view setup like:
<%= render layout: 'form' do |f| %>
<% for holiday in Holiday.find(:all) %>
<label class="checkbox">
<%= check_box_tag "user[holiday_ids][]", holiday.id, #user.holidays.include?(holiday) %>
<%= holiday.name %>
</label>
Which rendered a list of 8 "interests". However, I needed 3 out of these 8 to have unique div id's in order to have a jquery show/hide function to display divs below them when checked. To do this i just took the html rendered by the above erb code and pasted it into my view and manually modified it to work with the jquery statement (pasted below).. My question is that in these div's that are displayed by the jquery i need to have a dropdown which is populated by another model in my app.. How can i achieve this? I attempted the statement in the first div but its causing the following error (code taken from my _form partial):
undefined method `map' for nil:NilClass
Extracted source (around line #12):
9: </ul>
10: </div>
11: <% end %>
12: <%= yield f %>
13:
14: <div class="actions">
15: <%= f.submit "Continue" %>
Here is my _form Partial:
<%= form_for #user, url: wizard_path do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= yield f %>
<div class="actions">
<%= f.submit "Continue" %>
or <%= link_to "skip this step", next_wizard_path %>
</div>
<% end %>
Here is the HTML + erb statement i am attempting..
<%= render layout: 'form' do |f| %>
<div id="checkbox">
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="2" />
Valentine Day
</label>
</div>
<div style="display:none;">
<%= select_tag 'Interests', options_for_select(#interests) %>
</div>
<div id="checkbox">
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="4" />
Mothers Day
</label>
</div>
<div style="display:none;">
Whatever you have to capture here<input type="text" id="foo2" />
</div>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="7" />
Thanksgiving
</label>
<div id="checkbox">
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="5" />
Father's Day
</label></div>
<div style="display:none;">
Whatever you have to capture here<input type="text" id="foo3" />
</div>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="3" />
Easter
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="6" />
Halloween
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="8" />
Christmas
</label>
<% end %>
I think I had a similar problem with the view not seeing my variable, this is how I fixed it
<% #resources.each do |resource| %>
<%= render 'layouts/resources_expanded', :resource => resource %>
<% end %>
I've never seen <%= yield f %>
Don't you have to do:
<%= yield 'form', {:f => f} %>
Good Luck.