我有一個應用程序,允許用戶在名爲Projects的表中輸入和編輯項目。其中一個領域允許他們檢查該項目的技術。它們存儲在一個名爲Technols的單獨表格中,表格之間的關係稱爲Projecttechnols。Ruby on Rails:無法在某些場合更新表項
舉個例子,我有一個Project X,它有很多技術,tech1,tech2和tech3。如果我編輯項目,我可以刪除或添加項目的技術,並隨意更新項目。當我嘗試將所有項目從項目中拿出時,我的問題就開始了。如果我嘗試這個,沒有什麼改變,並且技術保持不變。
我沒有任何技術創建一個新項目沒有問題,我可以回到該項目並通過插入技術進行編輯,但是如果我再次使用添加的技術編輯它,並嘗試將其全部刪除。什麼都沒發生。
這裏是我的新的,編輯,從項目控制器創建和更新操作:
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
這裏是我的技術位編輯觀點:
<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>
我新的Rails,所以它可能是非常簡單的事情。所有的幫助將不勝感激。提前致謝。
感謝您抽出時間來解釋我哪裏出錯了。現在有道理。 – Jazz
沒問題。樂意效勞! –