I have an app that allows a user to enter and edit projects in a table called Projects. One of the fields allows them to check technologies for that project. They are stored in a separate table called Technols, and the relationship between the tables is called Projecttechnols.
As an example, I have a Project X, which has many technologies, tech1, tech2 and tech3. If I go into edit the project, I can remove or add the technolgies to the project, and update the project at will. My problem starts though, when I try to take all the projects out of project. If I try this, nothing is changed, and the technologies stay the same.
I have no problem creating a new project with no technologies, and I can go back into that project and edit by inserting technologies, but if I go back to edit it again with technologies added, and try to remove them all. Nothing happens.
Here are my new, edit, create and update actions from the project controller:
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.all
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
@project_technol = @project.projecttechnols.build
puts @project.inspect
puts @project.technols.inspect
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(params[:project])
@project.client = params[:new_client] unless params[:new_client].blank?
@project.role = params[:new_role] unless params[:new_role].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
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PUT /projects/1
# PUT /projects/1.json
# PUT /projects/1
# PUT /projects/1.json
def update
@project = Project.find(params[:id])
puts @project.inspect
puts @project.technols.inspect
params['project'][:client] = params[:new_client] unless params[:new_client].blank?
params['project'][:role] = params[:new_role] unless params[:new_role].blank?
params['project'][:industry] = params[:new_industry] unless params[:new_industry].blank?
params['project'][:business_div] = params[:new_business_div] unless params[:new_business_div].blank?
respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
Here is my edit view for the tech bit:
<div class="tech" STYLE="text-align: left;">
<b>Technologies:</b>
<style>
.split { text-align:left; }
</style>
<p>
<ul>
<% for technol in Technol.all %>
<li class="split">
<%= check_box_tag "project[technol_ids][]", technol.id, @project.technols.include?(technol) %>
<%= technol.tech %>
</li>
<% end %>
</ul>
</p>
I am new to Rails, so it might be something really simple. All help will be appreciated. Thanks in advance.
If the user unchecks all
project[technol_ids][]checkboxes, then that field doesn’t appear inparamsat all. At least one checkbox must be checked to have the field be present. So hopefully you can understand why the model doesn’t change any of theprojecttechnolrecords.To fix this, make sure that there is something stored for the param at the top of your
updateaction:This ensures that you have an empty array if no checkboxes are checked. Passing an empty array into the model should should clear out all records that are present.