I've incorporated the bsmSelect plugin to my Ruby on Rails application, and I'm having trouble saving the selections that I make. I've implemented bsmSelect to the point where I can define all of the data fields in my selection, I can add them to the list, delete them etc. (for an example see here - I basically have "Example 1" implemented: bsmSelect demo)
I'm having trouble tying in the "submit" button to the Ruby on Rails "Create" action. I want to take the items that are selected, click the "submit" button, and then create a new item with only those fields that I selected to be saved. I've defined the resource to be RESTful, and I've defined the create, new, and show methods in my controller.
Please help me to perform the "create" action which will display only the data fields in that model that I have selected using bsmSelect. Code snippets below:
views/application_templates/new.html.erb
<%= form_for(@apptemplate) do |f| %>
<%= render 'test', f: f %>
<%= f.submit "do it", class: "btn btn-large btn-primary" %>
<% end %>
'test' partial
<select name="Fields" multiple="multiple" title="Please select parameters">
<option><%= f.label :name %></option>
<option><%= f.label :email %></option>
<option><%= f.label :dob %></option>
</select>
controller
def show
@apptemplate=ApplicationTemplate.find(params[:id])
end
def new
@apptemplate=ApplicationTemplate.new
respond_to do |format|
format.html
format.js
end
end
def create #create a new property
@apptemplate= ApplicationTemplate.new
if @apptemplate.save
flash[:success] = "Template saved!"
redirect_to @apptemplate
else
render 'new'
end
end
Note: the example fields (name, email, dob) are of course defined in my model
So, assuming I selected just "name and DOB", how could I create a new AppTemplate that only showed that "Name and DOB" were saved by bsmSelect?
Thank you!!
In your 'test' partial you need to put this:
<select id="fields" name="fields[]" multiple="multiple" title="Please select parameters">
This will create a params[:fields] you can access in create method. Considering you have name, email and dob fields in your model, you could do something like this:
def create
@template = Template.new
params[:fields].each do |field|
@template[field.downcase] = true
end
if @template.save
redirect_to template_path(@template)
else
render 'new'
end
end
Notice "field.downcase". It's there because
<option><%= f.label :name %></option>
generates this in the view
<option>Email</option>