Reputation: 3
I created a "select_tag" field and I need it when the user clicks save all the values of this field to be passed by params to the controller. The problem is that when I click save, only the first value entered in this field is passed to the controller.
To better understand, the "select_tag" field is populated through another field, the "f.text_field :members" field. When I click on an "add" button the value of the "f.text_field :members" field is passed to the "select_tag" field and so I want to be able to check all these values from the "select_tag" field to validate and save, how do I do that? As I already said, only the first value entered is passed.
Looking at the params now, I realized that the value that returns as "params" is just the value that is selected in the dropdown, the other values are not considered. How do I pass all values in the dropdown?
Code:
<%= form_for(@focus_group) do |f| %>
<% if @focus_group.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@focus_group.errors.count, "error") %> prohibited this focus_group from being saved:</h2>
<ul>
<% @focus_group.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<script type="text/javascript">
$(function() {
$('#focus_group_moderator, #focus_group_members').autocomplete({
source: '/focus_groups/autocomplete.json'
});
});
function add(){
var value = $('#focus_group_members').val();
var select = document.getElementById("membersAdded");
var option = document.createElement("option");
option.text = value;
option.value = value;
select.add(option);
$('#focus_group_members').val("");
}
function removeFromSelect(){
var select = document.getElementById("membersAdded");
select.remove(select.selectedIndex);
}
</script>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :topic %><br>
<%= f.text_field :topic %>
</div>
<div class="field">
<%= f.label :moderator %><br>
<%= f.text_field :moderator %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :members %><br>
<%= f.text_field :members %>
<input onclick="add()" type="button" value="Add" /><br>
<%= select_tag(:membersAdded, options_for_select([])) %>
<input onclick="removeFromSelect()" type="button" value="Remove" /><br>
<br>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
CONTROLLER
class FocusGroupsController < ApplicationController
before_action :set_focus_group, only: [:show, :edit, :update, :destroy]
include FocusGroupsHelper
def index
if(params[:term])
@profile = Profile.all.select("name", "id")
@lista = Array.new
@profile.each do |x|
@lista.push(x.name)
end
respond_to do |format|
format.html
format.json { render json: @lista.to_json}
end
else
@focus_groups = FocusGroup.all
end
end
def show
end
def new
@focus_group = FocusGroup.new
@membersAdded
respond_to do |format|
format.html
format.json { render json: Profile.all.to_json}
end
end
def edit
end
def create
@moderator= find_profiles_id(focus_group_params[:moderator])
@moderator.each do |f|
@moderator_id = f.id
end
@params = focus_group_params
@params[:moderator] = @moderator_id
@focus_group = FocusGroup.new(@params)
if @focus_group.save
redirect_to @focus_group, notice: 'Focus group was successfully created.'
else
render :new
end
end
def update
if @focus_group.update(focus_group_params)
redirect_to @focus_group, notice: 'Focus group was successfully updated.'
else
render :edit
end
end
def destroy
@focus_group.destroy
redirect_to focus_groups_url, notice: 'Focus group was successfully destroyed.'
end
def autocomplete
if(params[:term])
@profile = Profile.all.where("user_id <> 0 and is_template = 'f' and name LIKE ?", "#{params[:term]}%").select("name", "id")
@lista = Array.new
@profile.each do |x|
@lista.push(x.name)
end
respond_to do |format|
format.html
format.json { render json: @lista.to_json}
end
end
end
def find_profiles_id(name)
return Profile.all.where("name LIKE ?", "#{name}%").select("id")
end
def find_profiles_name(id)
@profile = Profile.all.where("id = ?", "#{id}").select("name")
@profile.each do |e|
@name = e.name
end
return @name
end
private
def set_focus_group
@focus_group = FocusGroup.find(params[:id])
end
def focus_group_params
params.require(:focus_group).permit(:name, :topic, :moderator, :description, :members, :membersAdded)
end
end
Upvotes: 0
Views: 396
Reputation: 4443
Several ways to approach this -- but the select box itself doesn't make any sense to use in this situation. Provides no utility. Here's one way. Do you have a separate members model? It seems like you're essentially trying to do a nested form. If you don't have a members
model, create one. Create a focus group separately and put the form for members on the FocusGroup's show page. A focus group has many members and member belongs to a focus group (has a focus group id). This will allow you to say @focus_group.members and get all the members back.
If you must add them on the same form for some reason (I don't think this is optimal), you'll want to do so via a nested form with a form builder that allows you to add many at once. You could also make a service object but that is more complicated.
Another option, which I like less, would be to push all of the members to an array and then pass it as a param on submit. Instead of adding the members to the select box you'd just push them to an array. Then you would set the final array value to members_added (or whatever you name the parameter). You'll need to unpack that array in the controller to create each member. I like this much less. The first option is the cleanest. I think this is the third best approach of the three.
Upvotes: 0