控制器不更新我的DB(Rails)的更新方法

时间:2016-02-25 13:48:25

标签: ruby-on-rails ruby ruby-on-rails-4

在我的项目控制器中,我有一个更新方法由于某种原因我的更新方法不会更新我的数据库。任何帮助将不胜感激。

这是我的项目控制器。

def edit_production
  @project = Project.find(params[:id])
end

def edit_dfm
  @project = Project.find(params[:id])
end

def edit_prototype
  @project = Project.find(params[:id])
end

def edit_tooling
  @project = Project.find(params[:id])
end

def edit_quality
  @project = Project.find(params[:id])
end


def update
  @project = Project.find(params[:id])


  respond_to do |format|
    if @project.update_attributes(params[:p_params])
      format.html { redirect_to project_index_path, :notice => 'Project successfully updated.' }
      format.xml  { head :ok }
    end
  end
end

private

def p_params
  params.require(:project).permit(:project_id, :planner, :part_num, :status, :customer, status_attributes: [:a_d, :status, :note, :process]
end

以下是我对edit_production操作的看法,我想使用更新操作来更新我的数据库。

%table.table.table-bordered.trace-table
  %thead
    %tr.trace-table
      %th.ts.jp{:style => 'border: solid black; font-size:17px;'} New Project

= simple_form_for @project, :method => :patch do |f|
  = f.text_field :planner, :style => 'display:none', :input_html => {:value => current_user.name}
  %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
    %th.lt.feed Project ID:
    %th.lt.feed= f.text_field :project_id, :id => "project_id"

  %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
    %th.lt.feed Customer:
    %th.lt.feed= f.text_field :customer, :id => "cust_select"



    = f.simple_fields_for :status do |builder|
      = render "status", :f => builder
  %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}

   %th.lt.feed= f.submit "Submit"

这是我的路线

                        Prefix Verb     URI Pattern                            Controller#Action
                       signout GET|POST /signout(.:format)                     sessions#destroy
                        adauth GET|POST /adauth(.:format)                      sessions#create
                      sessions GET      /sessions(.:format)                    sessions#index
                               POST     /sessions(.:format)                    sessions#create
                   new_session GET      /sessions/new(.:format)                sessions#new
                  edit_session GET      /sessions/:id/edit(.:format)           sessions#edit
                       session GET      /sessions/:id(.:format)                sessions#show
                               PATCH    /sessions/:id(.:format)                sessions#update
                               PUT      /sessions/:id(.:format)                sessions#update
                               DELETE   /sessions/:id(.:format)                sessions#destroy
    pop_part_num_project_index GET      /project/pop_part_num(.:format)        project#pop_part_num
pop_customer_org_project_index GET      /project/pop_customer_org(.:format)     project#pop_customer_org
                 project_index GET      /project(.:format)                     project#index
                               POST     /project(.:format)                     project#create
                   new_project GET      /project/new(.:format)                 project#new
                  edit_project GET      /project/:id/edit(.:format)            project#edit
                       project GET      /project/:id(.:format)                 project#show
                               PATCH    /project/:id(.:format)                 project#update
                               PUT      /project/:id(.:format)                 project#update
                              DELETE   /project/:id(.:format)                 project#destroy
               production_edit GET|POST /project/:id/edit_production(.:format) project#edit_production
                      dfm_edit GET|POST /project/:id/edit_dfm(.:format)        project#edit_dfm
                prototype_edit GET|POST /project/:id/edit_prototype(.:format)  project#edit_prototype
                  tooling_edit GET|POST /entry/:id/edit_tooling(.:format)      project#edit_tooling
                 quality_edit GET|POST /entry/:id/edit_quality(.:format)      project#edit_quality
                          root GET      /                                      main#index

3 个答案:

答案 0 :(得分:1)

我认为罪魁祸首就是update方法中的这一行。

if @project.update_attributes(params[:p_params])

应该是

if @project.update_attributes(p_params)

答案 1 :(得分:1)

def update    
  @project = Project.find(params[:id])    
    if @project.update_attributes(p_params)   
          redirect_to @project    
          flash[:success] = "Task Updated!"   
    else  
          render action: :edit   
    end   
end  

分配属性时,必须将哈希作为参数传递。

答案 2 :(得分:0)

使用此代码:

@project.update_attributes(p_params)