Rails 3嵌套编辑表单不更新

时间:2012-11-06 22:01:41

标签: ruby-on-rails-3 forms nested-form-for

我有一个嵌套属性的表单。当我点击更新时,它会正确重定向并说“成功”,但属性不会更新。我尝试了几种不同的form_for组合,但尚未成功更新。

我用于新部门的表单不同,form_for看起来像这样

<%= form_for([@client,@client.departments.build]) %> 

此表单嵌套在客户端#show上。

编辑表格

    <%= form_for([@client, @department], :url => client_department_path(@client,@department)) do |f| %> 
      <div class="field">                                                                               
        <%= f.label :department_name %><br />                                                           
        <%= f.text_field :department_name %>                                                            
      </div>                                                                                            
      <div class="field">                                                                               
        <%= f.label :department_manager %><br />                                                        
        <%= f.text_field :department_manager %>                                                         
      </div>                                                                                            
      <div class="actions">                                                                             
        <%= f.submit %>                                                                                 
      </div>                                                                                            
    <% end %>      

控制器

    class DepartmentsController < ApplicationController

before_filter :get_client
def get_client
  @client = Client.find(params[:client_id])
end 
def create
    @department = @client.departments.create(params[:department])
    redirect_to client_path(@client)

end
def update
  @department = @client.departments.find(params[:id])

  respond_to do |format|
  if @department.update_attributes(params[:task])
  format.html { redirect_to client_path(@client), notice: 'Department was successfully updated.' }
  format.json { head :no_content }
  else
  format.html { render action: "edit" }
  format.json { render json: @department.errors, status: :unprocessable_entity }
  end
end
end
def show
  @department = @client.departments.find(params[:id])


    respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @client }
    end
end
def edit
    @department = @client.departments.find(params[:id])
end

def destroy
    @client = Client.find(params[:client_id])
    @department = @client.departments.find(params[:id])
    @department.destroy
    redirect_to client_path(@client)
end     
end

0 个答案:

没有答案
相关问题