Reputation: 1090
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 was given help earlier, and it was suggested that I should add this:
def create
...
technol_ids = params[:technol_ids].blank? ? [18] : params[:technol_ids]
technol_ids.each do |id|
...
end
...
end
I am having trouble getting this to work. I don't think I am putting it in the right bit of my code. I'm still new to rails, so please remember this when trying to help. Thanks very much
EDIT
With no technols_id selected
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"8Hgc1GXqNhWkzO3Wgkfpf6z+fdImf6QvAv0XLbP0a5g=", "project"=>{"project
_name"=>"Test", "archive"=>"0", "username"=>"test", "status"=>"Active", "exception_pm"=>"", "client"=>"", "business_d
iv"=>"", "project_owner"=>"", "start_date"=>"11-12-2012", "first_name"=>"test", "last_name"=>"test", "fullname"=>
"test test", "entry_date"=>"2012-11-26", "end_date"=>"19-12-2012", "techinfo"=>"Test", "role"=>"", "industry"=>""
, "summary"=>"Test", "lessons_learned"=>"Test", "customer_benefits"=>"Test", "financials"=>"£500,000 - £999,999 "}, "n
ew_exception_pm"=>"Test", "new_client"=>"Test", "new_business_div"=>"Test", "new_project_owner"=>"Test", "new_role"=>"Te
st", "new_industry"=>"Test", "commit"=>"Save New Project"}
With 1 technols_id
Started POST "/projects" for 192.168.16.127 at 2012-11-26 16:47:27 +0000
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"8Hgc1GXqNhWkzO3Wgkfpf6z+fdImf6QvAv0XLbP0a5g=", "project"=>{"project
_name"=>"Test", "archive"=>"0", "username"=>"Test", "status"=>"Active", "exception_pm"=>"", "client"=>"", "business_d
iv"=>"", "project_owner"=>"", "start_date"=>"11-12-2012", "first_name"=>"Test", "last_name"=>"McLaughlin", "fullname"=>
"Test Test", "entry_date"=>"2012-11-26", "end_date"=>"20-12-2012", "technol_ids"=>["1"], "techinfo"=>"Test", "rol
e"=>"", "industry"=>"", "summary"=>"Test", "lessons_learned"=>"Test", "customer_benefits"=>"Test", "financials"=>"£250,
000 - £499,999 "}, "new_exception_pm"=>"Test", "new_client"=>"Test", "new_business_div"=>"Test", "new_project_owner"=>"
Test", "new_role"=>"Test", "new_industry"=>"Test", "commit"=>"Save New Project"}
Upvotes: 1
Views: 437
Reputation: 1340
First of all, the name of the field should be used to get the parameters, use like my example. You are receiving an array of tech ids, right? So instead of using if to check nil, use this:
params[:project][:technol_ids] ||= [18]
It will turn this parameter into array if it is nil.
params[:project][:technol_ids].each do |tech_id|
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
Upvotes: 3