I have an app where a user can enter a project into a database.
There is an option where they can select a number of different technologies for their project. At the moment, the app flags up an error if the user doesn’t select at least 1 technology.
I am wanting to change this so that if they don’t select a technology, it automatically goes down as “other” instead.
Here is my project controller actions, new and create:
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.order('tech ASC')
@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
def create
@project = Project.new(params[:project])
@project.client = params[:new_client] unless params[:new_client].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
if !params[:technols].nil?
params[:technols][:id].each do |tech|
if !tech.empty?
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
end
end
Here is the new view for the technology field
<ul>
<% @all_technols.each do |technol| %>
<li class="split">
<%= check_box_tag "project[technol_ids][]", technol.id, @project.technols.include?(technol) %>
<%= technol.tech %>
</li>
<% end %>
</ul>
The technology ID for “other” in the technols table is “18”. So is there a way to say that if no technology is chosen, then :technol_id => ["18"].
I’m still new to rails, so please remember this when trying to help. Thanks very much
You have two choices.
First, add an hidden input to the form:
Second, update params in controller#action: