Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:59:26+00:00 2026-05-23T06:59:26+00:00

Say I have a formtastic form that creates a Store and allows selection of

  • 0

Say I have a formtastic form that creates a Store and allows selection of Services it provides:

<%= semantic_form_for @store do |f| %>
  <%= f.inputs :services, :as => :check_boxes, :collection => Service.all %>
  <%= f.buttons %>
<% end -%>

I want to allow the user to add a new Service in case he doesn’t see it in the options, right from the form.

There are many examples for simple nested form element addition, say of a Task entry to a Project, and even a gem that helps with this, but I haven’t found any that create a new resource so it’ll show as part os checkbox or select options.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:59:27+00:00Added an answer on May 23, 2026 at 6:59 am

    Got it working this way:

    <%= semantic_form_for @store do |f| %>
      <%= f.inputs :services, :as => :check_boxes, 
                              :collection => Service.all,
                              :wrapper_html => { :id => 'service_fields' } %>  
      <%= f.buttons %>
    <% end -%>
    

    Added an id to the parent list item around the checkbox field listing, so it can be accessed by this js after submitting a text field with the new service name:

    <input type="text" id="new_service_name" />
    <input type="button" value="ok" id="btnSave" />                                
    
    <script type="text/javascript">        
      $(document).ready(function() {
        $('#btnSave').click(function() {
          $.ajax({
            url: '/admin/services.json',
            type: 'POST',
            dataType: 'json',
            data: 'service[name]=' + $('#new_service_name').val(),
              success: function(data) {
                addCheckbox(data);
              }
            });
          });
        });
    
        function addCheckbox(name) {
          var container = $('#service_fields fieldset ol');
          var inputs = container.find('input');
          var id = inputs.length+1;
    
          //var html = '<input type="checkbox" id="cb'+id+'" value="'+name+'" /> <label for="cb'+id+'">'+name+'</label>';
          var html = '<li><label for="store_services_'+id+'"><input id="store_services_'+id+'" name="store[services][]" type="checkbox" value="'+id+'" />'+name+'</label></li>';
          container.append($(html));
        }
    </script>
    

    Then, in ServicesController:

    class ServicesController < ApplicationController  
      respond_to :json
    
      def create
        service = Service.create!(params[:service])    
        respond_with(service)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say have a linear model LM that I want a qq plot of the
Say I have an HTML form like this to collect an email from a
Say I have a class with synchronized methods. It's actually a class that uses
Say i have one activity that gets a cookie and then starts a new
Say I have a simple function that alerts a message: function callMessage(msg){ alert(msg); }
Say I have a method that returns this. Vector[ (PkgLine, Tree) ]() I want
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.