2015-05-25 44 views
0

我新的軌道和有麻煩我的編輯表單更新屬性。的Rails的form_for編輯時不保存更改

這裏是我的項目,並注意的routes.rb:

這是我在查看/筆記/ edit.html.erb的form_for:

 <%= form_for [@project, @note] do |f| %> 

     <div class="form-group"> 
      <%= f.label :title %> 
      <%= f.text_field :title, class: "form-control" %> 
     </div> 

     <div class="form-group"> 
      <%= f.label :body %> 
      <%= f.text_area :body, rows: 10, class: "form-control" %> 
     </div> 

     <div class="action"> 
      <%= f.submit "Save Note", class: "btn btn-success" %> 
     </div> 
     <% end %> 

最後,這是我的notes_controller.rb:

class NotesController < ApplicationController 

    before_action :find_note, only: [:show, :edit, :update, :destroy] 

    def index 
     @notes = Note.all 
    end 

    def new 
     @project = Project.find(params[:project_id]) 
     @note = @project.notes.new 
    end 

    def create 
     @project = Project.find(params[:project_id]) 
     @note = @project.notes.build(note_params) 
     @note.project = @project 
     @note.save ? flash[:notice] = "Note created." : flash[:error] = "Note could not be created, please try again." 
     redirect_to [current_user, @project] 
    end 

    def show 
    end 

    def edit 
    end 

    def update 
     if @note.update_attributes(note_params) 
     @note.save 
     flash[:notice] = "Note updated." 
     redirect_to authenticated_root_path 
     else 
     flash[:error] = "Could not update note." 
     render :edit 
     end 
    end 

    def destroy 
     @note.delete ? flash[:notice] = "Note deleted." : flash[:error] = "Note could not be deleted." 
     redirect_to user_project_path 
    end 

    private 

    def note_params 
     params.require(:note).permit(:title, :body) 
    end 

    def find_note 
     @project = Project.find(params[:project_id]) 
     @note = @project.notes.find(params[:id]) 
    end 
    end 

這是我的終端輸出:

Started GET "/projects/12/notes/13/edit?utf8=%E2%9C%93&_method=patch&authenticity_token=vnIoOSi0ksMNI7uU0aMnBpkTBWi75wy%2BYvbs3RxO5sPIbvFzDA30%2B32dJxurdcsiu9zsIpuDCR%2FARUamBRrYmg%3D%3D&note%5Btitle%5D=Transporter+Materials&note%5Bbody%5D=Star+dust%2C+data&commit=Save+Note" for 127.0.0.1 at 2015-05-25 08:38:16 -0700 
Processing by NotesController#edit as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"vnIoOSi0ksMNI7uU0aMnBpkTBWi75wy+Yvbs3RxO5sPIbvFzDA30+32dJxurdcsiu9zsIpuDCR/ARUamBRrYmg==", "note"=>{"title"=>"Transporter Materials", "body"=>"Star dust, data"}, "commit"=>"Save Note", "project_id"=>"12", "id"=>"13"} 
    Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 12]] 
    Note Load (0.2ms) SELECT "notes".* FROM "notes" WHERE "notes"."project_id" = ? AND "notes"."id" = ? LIMIT 1 [["project_id", 12], ["id", 13]] 
    Rendered notes/edit.html.erb within layouts/application (3.4ms) 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
Completed 200 OK in 328ms (Views: 296.4ms | ActiveRecord: 2.3ms) 

在此先感謝您的任何建議。

回答

0

如果您正在使用@project.notes.build(note_params)。 不需要這個@note.project = @project中,如果你使用的是@note.update_attributes(note_params) 這不是必需的@note.save 和更好的請嘗試使用調試它創建行動

,並在更新

@note.update_attributes!(note_params)

它會給你爲什麼它無法保存。

<%= form_for [@project, @note], :url => project_note_path(@project,@note), :method => :put do |f| %>

+0

雖然這確實提供了改進,它不提供該問題的原因。正在發出GET請求。 –

+0

@ d端GET請求已經發出你是對的,但對編輯操作不進行更新。 –

+0

是的,完全相同的頁面這種形式的。 –