My code is as below :
<% for market_language in #pyr_market_languages %>
<%= f.input :"pyr_crm_call_script[market_language_ids][]", :as => :check_box, input_html:{value: market_language.id}, #callscript.market_languages.include?(market_language) %>
<%= market_language.name %><br/>
<% end %>
I just want to use simple form tag for checkbox like f.input,because i couldn't see the errors for checkbox field even if it is mandatory.
Can any one of you provide the way of using checkbox in a simple form in rails.
Related
I have a form for which I am using checkboxes (Not using radio buttons for my purpose). The problem I run into is when I submit a form, I get an error saying params is missing or value is empty:checkup. I am trying to use hidden filed but get the same error. HOw to have an option of sending only one if selected?
def checkup_params
params.require(:checkup).permit(:eye, :heart)
end
my form:
<%= form_for(#checkup) do |f| %>
<%= hidden_field_tag "checkup[eye]", nil %>
<%= check_box_tag :eye, "eye" %>
<%= hidden_field_tag "checkup[heart]", nil %>
<%= check_box_tag :heart, "heart" %>
<%= f.submit %>
<% end %>
I suggest reading this guide http://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects
Most specifically section 2.2. You really shouldn't be using tag helpers here.
What I have now gives me a dropdown menu where I can only select one:
<%= form_for(#submission) do |f| %>
<%= f.collection_select :id, Submission::SUB_ID, :to_s, :to_s %>
<% end %>
where SUB_ID=[1,2,3] in model Submission
I want to implement a checkbox instead of a dropdown menu so that I can select multiple SUB_ID (i.e. 1&2 or 1&3 or 2&3 or 1&2&3). I tried to use this but it does not work:
<%= f.check_box :id, Submission::SUB_ID, :to_s, :to_s %>
Any idea?
Try this:
# view
<%= form_for(#submission) do |f| %>
<%= Submission::SUB_ID.each do |sub_id| %>
<%= f.checkbox 'ids[]', value: sub_id, checked: #submission.id == sub_id %>
<%= sub_id %>
<% end %>
<% end %>
# controller
params[:submission][:ids].each do |checked_sub_id|
# do your logic here
end
you have to iterate over SUB_ID
somehow like this...
<% Submission::SUB_ID.each do |ssid| %>
<%= f.check_box "ids[]", value: ssid %>
<% end %>
or you can use formtastic gem. it has :as=>:check_boxes input fields http://www.ruby-doc.org/gems/docs/n/nuatt-formtastic-0.2.3/Formtastic/Inputs/CheckBoxesInput.html
The core answer is you need to loop over each item in Submission::SUB_ID and make a checkbox for each id. Depending on how your models are set up and what you want to do - you may need to be much more involved in the form building. I hesitate to provide specific examples without know more about how you want the data to come back to the controller
<%= form_for(#submission) do |f| %>
<% Submission::SUB_ID.each do sub_id %>
<%= f.check_box_tag 'submission_ids[]', sub_id %>
<% end %>
<% end %>
Note that that will not default anything to checked and it does not come back as part of the submission parameters.
Usually when I have a similar situation I'm using nested forms to add or remove objects.
If you're using Rails 4, there is a new helper, collection_check_boxes, which helps streamline the building of your check boxes.
<%= f.collection_check_boxes :submission_ids, Submission::SUB_ID, :to_s, :to_s %>
Documentation:
Form builder version - which wraps...
...the general form options helper
If you look at the documentation in the second link, you'll also find how to use the optional block syntax to customise the HTML structure for each check box.
I have this code in my form :
guidances/new.html.erb
<%= f.association :employee_department, as: :select %>
<%= f.input :guidance_supervisor do %>
<%= f.select :guidance_supervisor,
Employee.where('guidance_supervisor' => true).map(&:full_name) %>
<% end %>
and when selecting a department I need to make the employees who belongs to that department appears, I'm using simple_form , what is the way to make this happen ?
For this you can use dynamic drop-down concept. You can see the following post for dynamic drop down in rails:
Dynamic drop down in rails.
This isn't working as I'd like.
I have an entry form for a new job, which is made up of a number of steps. By default there are 4 new and unsaved steps populated per job.
Using the code from the view below, I am able to attach the jQuery UI datepicker control to the text fields and they appear against the correct input field when i click them.
However, when I select a date from the picker it is only ever going into the field representing the start date of the first step.
<%= form_for #job, :url => jobs_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<% #job.steps.each do |step| %>
<%= fields_for "job[step_attributes][]", step do |s| %>
<%= s.label :name %>
<%= s.text_field :name %>
<%= s.label :start_date %>
<%= s.text_field :start_date, :class => :datepicker %>
<%= end %>
<% end %>
<script type="text/javascript">
$(function() {
$('.datepicker').datepicker({ dateFormat: "dd/mm/yy"});
});
</script>
I know ultimately this has to do with the generated id attribute for the input elements being the same, was wondering, has anybody successfully overcome this issue?
This fiddle: http://jsfiddle.net/twilson/u9m9L/ demonstrates my problem.
First, invoke fields_for on your form builder instead:
<%= f.fields_for :steps do |s| %>
Second, you don't need to iterate through #job.steps if you specify the association name to fields_for. There's an example on how to use it with one-to-many assoiciations here.
If you still have this problem, paste your generated HTML, it would easier to find the cause.
EDIT
OK so the problem is because Rails form builder does not generate child indexes to give unique IDs to nested fieldsets. This most likely happened because the associations are built but not yet saved to the DB. One way I see is to assign child indexes manually, like so:
<% #job.steps.each_with_index do |step,i| %>
<%= f.fields_for :steps, step, :child_index => i do |s| %>
See if that helps.
This is a somewhat contrived scenario given that you have only new objects. To be able to dynamically add/remove nested items, this would be a bit trickier. You can see how this can be dealt with in the nested model Railscasts.
Say I have a Rails form like the following
<% form_for #model do |f| %>
<%= f.label :column %>
<%= f.check_box :column %>
<% end %>
Is there a way I can get the html id that will be generated for the 'column' check box? What would be great is if there was a way to add in
<%= f.observe_field :column, options %>
Anyone know how to add this to FormBuilder?
For generating an element's id programatically, see this. As it seems, there is no easy way out.
You could add this to FormBuilder by wrapping up certain field tags. See for example this.